2013-07-06 35 views
0

现在我正在学习C编程课程,所以我完全是C的新手。我现在头痛了,因为我的代码不能按我认为的那样工作。C - 使用getchar()读取意外字符

这里是我的代码,显示问题:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <ctype.h> 
#include <string.h> 

int main() 
{ 
    printf("\n\nList of Paycodes:\n"); 
    printf("1 Manager\n"); 
    printf("2 Hourly worker\n"); 
    printf("3 Commission Worker\n"); 
    printf("4 Cook\n\n"); 

    bool cd=true; 
    float weekmoney; 
    char name[100]; 
    char code[10]; 
    int codeint; 
    char cl; 
    int cllen; 
    char hw[5]; 
    int hwint; 
    int salary=0; 

    while(cd){ 
     printf("Enter employee name: "); 
     fgets(name,100,stdin); 
     name[strlen(name)-1]='\0'; // nk remove new line lepas user input 
     printf("Enter employee\'s paycode: "); 
     strcpy(code, ""); 
     fgets(code, 10, stdin); 
     codeint = atoi(code); 
     if(codeint > 4 || codeint <= 0){ 
      printf("\nPlease enter correct employee\'s paycode!\n\n"); 
      continue; 
     }else if(codeint == 1){ 
      printf("%s\'s pay for this week (RM): 500.00\n\n", name); 
     }else if(codeint == 2){ 
      printf("Enter hours work this week: "); 
      fgets(hw, 5, stdin); 
      hwint=atoi(hw); 
      if(hwint > 12){ 
       hwint -= 12; 
       salary += 500; 
      } 
      if(hwint > 0){ 
       for(int i=0;i < hwint;i++){ 
        salary += 100; 
       } 
      } 
      printf("%s\'s pay for this week (RM): %d\n\n", name, salary); 
     }else if(codeint == 3){ 
      printf("Enter %s\'s this week sales (RM): ", name); 
      scanf("%f",&weekmoney); 
      printf("%s\'s pay for this week (RM): %.1f\n", name, (((5.7/100)*weekmoney)+250)); 
     } 
     while(true){ 
      printf("Do you wish to continue? (Y = Yes, N = No): "); 
      cl=getchar(); 
      getchar(); 
      if(tolower(cl) == 'y'){ 
       break; 
      }else if(tolower(cl) == 'n'){ 
       cd=false; 
       break; 
      }else{ 
       printf("\nPlease enter correct value!\n\n"); 
       continue; 
      } 
     } 
    printf("\n"); 
    } 
} 

这是问题所在和解释。

如果通过这部分代码运行我的代码

printf("Enter %s\'s this week sales (RM): ", name); 
scanf("%f",&weekmoney); 
printf("%s\'s pay for this week (RM): %.1f\n", name, (((5.7/100)*weekmoney)+250)); 

这里

cl=getchar(); 
getchar(); 
if(tolower(cl) == 'y'){ 
    break; 
}else if(tolower(cl) == 'n'){ 
    cd=false; 
    break; 
}else{ 
    printf("\nPlease enter correct value!\n\n"); 
    continue; 
} 

会得到一个错误此代码,但如果我的代码不通过的问题部分运行,它的工作原理好。 我试图找到解决方案+调试近一个小时,但仍然没有找到正确的解决方案来解决我的问题。

+0

请注意,当调试这样的问题时,它可以帮助打印出您发现的字符是'错误的'。例如,'printf(“Got%d(%c)\ n”,cl,isprint(cl)?cl:'。');'会打印出意想不到的字符,并且看到它打印出10(换行符)给你一个关于麻烦是什么的想法。您还应该检查EOF(并从'scanf()'返回值)。你需要改变'cl'的类型;它应该是一个'int'来允许精确处理EOF(因为'getchar()'返回一个'int',而不是'char',因为它必须返回每个'char'值加上EOF)。 –

回答

0

Put getchar();除去新行缓冲scanf函数

后,所以这将是这样

printf("Enter %s\'s this week sales (RM): ", name); 
scanf("%f",&weekmoney); 
getchar(); 
printf("%s\'s pay for this week (RM): %.1f\n\n", name, (((5.7/100)*weekmoney)+250)); 

感谢俞灏在缓冲解释新的生产线。 :D

+1

只要用户在每周销售价值后没有输入任何空格(或其他字符),它就会生效。通常使用'fgets()'来读取输入行,然后使用'sscanf()'来解析它。 –

2
scanf("%f",&weekmoney); 

在这里,当你输入一个号码,然后按ENTER键,scanf将处理该号码,但新生产线尚处于缓冲,并且将通过以下getchar进行处理。您可以使用以下内容来匹配\n

scanf("%f%*[\n]", &weekmoney); 

然而,scanf有很多问题,使用像fgets其他功能可能时。

+0

如何避免缓冲区中仍然存在新行? –

+0

你的* [\ n]不适合我。 但是,把getchar();在scanf(“%f”,&weekmoney)之后;对我来说工作得很好,它解决了我的问题:D –

+0

请注意,'%* [\ n]'是抑制的指派扫描集转换规范;小心使用'%'。 –