2013-07-06 33 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]'是抑制的指派掃描集轉換規範;小心使用'%'。 –