字符串比较有问题

时间:2018-12-02 23:20:29

标签: c string output fgets strcmp

这是我到目前为止的代码:

#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

我不确定问题是否出在“轮胎旋转”字符串比较上?如果有,我似乎找不到。解决该问题的任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

  

我不确定问题是否出在“轮胎旋转”字符串比较上?

strcmp(userService1,"Tire rotation\n")由于fgets(userService1,13,stdin);中有13个数字,因此无法比较,我希望至少为15个。

13允许fgets()读取最多12个字符,其余部分留在stdin中以弄乱下一个输入。

使用fgets(userService1, sizeof userService1,stdin);是因为char userService1[50];足够大。

答案 1 :(得分:1)

您有两个问题:

  1. 您误用了fgets()。您似乎正在将该函数的第二个参数指定为有效响应中期望的最大字符数,但是

    1. 您应改为指定指定缓冲区中的可用字节数(在这种情况下为50)。
    2. 该函数使用一个字节作为字符串终止符,从而将实际数据的字节数减少了一个。
    3. 在考虑了换行符和终止符后,实际上需要15个字节才能成功读取整个字符串“轮胎旋转\ n”。
    4. 您似乎假设fgets()读取了整行。那不是真的。是的,它在读取换行符时停止读取,是的,但是当它在目标缓冲区中空间不足时,它也会停止读取。当后者在前者之前时,该行的尾部留给下一个fgets()读取。
  2. 您用于第二个服务的字符串比较集省略比较字符串的结尾换行符。

因此,告诉fgets()真正的缓冲区长度并始终如一地正确处理换行符应该解决您的问题。