Reputation: 11
This is the code I have so far:
#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;
}
when the input is:
Tire rotation
Car wash
The output is:
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
When it should be:
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
When any other combination of services is entered, such as:
Oil change
Car wax
It works correctly and outputs how is it supposed to:
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 problem is with the Tire Rotation string comparison? If there is, I cant seem to find it. Any help with solving this problem would be appreciated!
Upvotes: 0
Views: 146
Reputation: 181824
You have two problems:
You are using fgets()
a bit wrongly. You appear to be specifying the second parameter to that function as the maximum number of characters you expect in a valid response, but
fgets()
reads a whole line. That's not exactly true. It stops reading when it reads a newline, yes, but it also stops reading when it runs out of room in the target buffer. When the latter precedes the former, the tail of the line is left to be read by the next fgets()
.Your set of string comparisons for the second service omit the trailing newline for the comparison strings.
So, telling fgets()
the true buffer length and providing consistently correct handling of the newlines should take care of your problems.
Upvotes: 1
Reputation: 154562
I'm not sure if the problem is with the Tire Rotation string comparison?
strcmp(userService1,"Tire rotation\n")
cannot compare as equal due to 13 in fgets(userService1,13,stdin);
I'd expect at least 15.
The 13
allows fgets()
to read in unto 12 characters, leave the remainder in stdin
to mess up the next input.
Use fgets(userService1, sizeof userService1,stdin);
as char userService1[50];
is big enough.
Upvotes: 2