Это код, который я написал:
#include <stdio.h>
#include <string.h>
int main(void) {
char userService1[50];
char userService2[50];
int price1;
int price2;
printf("Davy's auto shop services\n\n");
printf("Select first service:\n");
fgets(userService1,13,stdin);
printf("Select second service:\n\n");
fgets(userService2,13,stdin);
printf("Davy's auto shop invoice\n\n");
if(strcmp(userService1,"Oil change") == 0){
printf("Service 1: Oil change, $35\n");
price1 = 35;
}
else if(strcmp(userService1,"Tire rotation") == 0){
printf("Service 1: Tire rotation, $19\n");
price1 = 19;
}
else if(strcmp(userService1,"Car wash") == 0 ){
printf("Service 1: Car wash, $7\n");
price1 = 7;
}
else if(strcmp(userService1,"Car wax") == 0 ){
printf("Service 1: Car wax, $12\n");
price1 = 12;
}
else{
printf("Service 1: No service\n");
price1 = 0;
}
if(strcmp(userService2,"Oil") == 0){
printf("Service 2: Oil change, $35\n\n");
price2 = 35;
}
else if(strcmp(userService2,"Tire") == 0 ){
printf("Service 2: Tire rotation, $19\n\n");
price2 = 19;
}
else if(strcmp(userService2,"Car") == 0 ){
printf("Service 2: Car wash, $7\n\n");
price2 = 7;
}
else if(strcmp(userService2,"Car") == 0 ){
printf("Service 2: Car wax, $12\n\n");
price2 = 12;
}
else{
printf("Service 2: No service\n\n");
price2 = 0;
}
int price = price1 + price2;
printf("Total: $%d\n", price);
return 0;
}
Это то, что выводится с текущей версией:
Davy's auto shop services
Oil change -- $35
Tire rotation -- $19
Car wash -- $7
Car wax -- $12
Select first service:
Select second service:
Davy's auto shop invoice
Service 1: No service
Service 2: No service
Total: $0
с вводом:
Oil change
Car wax
Это желаемый вывод для кода:
Davy's auto shop services
Oil change -- $35
Tire rotation -- $19
Car wash -- $7
Car wax -- $12
Select first service:
Select second service:
Davy's auto shop invoice
Service 1: Oil change, $35
Service 2: Car wax, $12
Total: $47
Я не уверен, неправильно ли назначаются строки с помощью fgets()
или, если строки сравниваются неправильно.В любом случае я не знаю, как решить проблему.