0

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.

Cheeseriot
  • 11
  • 1
  • 2
    you need to trim the `\n` from `fgets`. ie: `fgets` returns inputted string with `\n` appended at the end. – kiran Biradar Dec 02 '18 at 07:51
  • Possible duplicate of [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – kiran Biradar Dec 02 '18 at 07:54
  • Note: This is a great time to learn how to use a debugger to step through the code line by line, checking variable contents etc to see what is actually happening. – Sami Kuhmonen Dec 02 '18 at 08:00
  • Also, consider using 50 as `fgets`'s second argument as both the arrays are of size 50 – Spikatrix Dec 02 '18 at 08:02

1 Answers1

0

The issue is that fgets reads "\n" at the end of the string

A quick fix would be to include it in your comparison:

if(strcmp(userService1,"Oil change\n") == 0){
    printf("Service 1: Oil change, $35\n"); 
    price1 = 35; 
}

Or you can cut out the "\n" entirely like so:

if(strcmp(strtok(userService1,"\n"),"Oil change") == 0){
    printf("Service 1: Oil change, $35\n"); 
    price1 = 35; 
}

Both solutions gave the intended result.

Jad Mrad
  • 53
  • 1
  • 7