Это код, который у меня до сих пор:
#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\nOil change -- $35\nTire rotation -- $19\nCar wash -- $7\nCar wax -- $12\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\n") == 0){
printf("Service 1: Oil change, $35\n");
price1 = 35;
}
else if(strcmp(userService1,"Tire rotation\n") == 0){
printf("Service 1: Tire rotation, $19\n");
price1 = 19;
}
else if(strcmp(userService1,"Car wash\n") == 0 ){
printf("Service 1: Car wash, $7\n");
price1 = 7;
}
else if(strcmp(userService1,"Car wax\n") == 0 ){
printf("Service 1: Car wax, $12\n");
price1 = 12;
}
else{
printf("Service 1: No service\n");
price1 = 0;
}
if(strcmp(userService2,"Oil change") == 0){
printf("Service 2: Oil change, $35\n\n");
price2 = 35;
}
else if(strcmp(userService2,"Tire rotation") == 0 ){
printf("Service 2: Tire rotation, $19\n\n");
price2 = 19;
}
else if(strcmp(userService2,"Car wash") == 0 ){
printf("Service 2: Car wash, $7\n\n");
price2 = 7;
}
else if(strcmp(userService2,"Car wax") == 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;
}
, когда вход:
Tire rotation
Car wash
Выход:
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
Когда этодолжно быть:
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: Tire rotation, $19
Service 2: Car wash, $7
Total: $26
Когда вводится любая другая комбинация услуг, например:
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
Я не уверен, что проблема в сравнении строк в Tire Rotation?Если есть, я не могу найти это.Буду признателен за любую помощь в решении этой проблемы!