This is the code I have written:
#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;
}
This is what is being outputted with the current version:
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
with the intput:
Oil change
Car wax
This is the desired output for the code:
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
I'm not sure if the strings are being assigned incorrectly with fgets()
or, if the strings aren't being compared correctly. Either way I do not know how to solve the problem.