2012-02-03 42 views
4

我似乎無法弄清楚什麼是錯,此代碼:fflush(標準輸入)功能不起作用

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


#define MAX 100 
#define TRUE 1 
#define FALSE 0 

char sect_cat; 
char customer_name[MAX]; 
char customer_number[MAX];  /* error handling is easier */ 

int prev_unit = 0; 
int current_unit = 0; 
int consumed = 0; 
int set = FALSE; 

float init_bill; 
float tax; 
float total_bill; 


    void get_userinfo() 
    { 

      printf("Enter sector category: "); 
      scanf("%c", &sect_cat); 
     printf("Enter customer name: "); 
     fflush(stdin); 
     scanf("%sn", &customer_name); 

     set = FALSE; 
     while (set == FALSE) 
     { 
      printf("Enter customer number: "); 
      fflush(stdin); 
      scanf("%s", customer_number); 

      int i; 
      int error; 
      for (i=0, error=0; i<strlen(customer_number); i++) 
      { 
       if (isdigit(customer_number[i])) 
       { 
       } 
       else 
       { 
        error = 1; 
       } 
      } 
      if (error == 0) 
      { 
       set = TRUE; 
      } 
      else 
       printf("ERROR: Only numbers are allowed\n"); 
     } 

     printf("Enter previous unit: "); 
     fflush(stdin); 
     scanf("%d", &prev_unit); 

     set = FALSE; 
     while (set == FALSE) 
     { 
      printf("Enter current unit: "); 
      fflush(stdin); 
      scanf("%d", &current_unit); 

      if (prev_unit > current_unit) 
      { 
       printf("ERROR: Current unit must be larger than previous unit\n"); 
      } 
      else 
       set = TRUE; 
     } 
     consumed = current_unit - prev_unit; 
    } 



int main() 
{ 


/* Introduce program to users */ 

     printf("\nThis program computes your electric bill based on these sector categories\n\n"); 

    printf("\tResidential(R)\n"); 
    printf("\tIndustrial(I)\n"); 
    printf("\tCommercial(C)\n\n"); 

    printf("Press any key to continue..."); 
    fflush(stdin); 
    getchar(); 
################### #編輯

應用templatetypedef的解決方案,程序現在等待用戶輸入customer_name。但是,輸入帶空格的字符串會導致出現錯誤,並且程序會假定該空格之後的單詞是爲下一個提示輸入的。

Enter sector category: r 
Enter customer name: George of the Jungle 
Enter customer number: ERROR: Only numbers are allowed 
Enter customer number: ERROR: Only numbers are allowed 
Enter customer number: 
+7

'fflush(標準輸入)'是未定義的行爲。 – Mysticial 2012-02-03 01:30:20

+3

將來,不要轉儲一個龐大的程序(沒有行號或指示相關信息的位置),請嘗試發佈代碼示例,以儘可能少的無關代碼來演示相同的問題,以便我們可以更清楚地幫你。 (另外,你甚至可能最終自己解決它!) – 2012-02-03 01:44:22

回答

1

我認爲你的意思是寫fflush(stdout)而不是fflush(stdin)

+2

我不認爲這是OP的意思。我認爲OP希望'fflush(stdin)'清除'stdin'中的換行符,這樣後面的讀操作就不會拾取它。 – templatetypedef 2012-02-03 01:33:57

4

fflush函數不會從輸入流中清除數據;它用於將輸出流中緩衝的數據推送到目標。這記錄在here。如this earlier SO question所示,嘗試使用fflush(stdin)會導致未定義的行爲,因此最好避免它。

如果你想吃從返回字符的換行符進入當用戶完成了他們的性格打字,而不是考慮以下因素:

scanf("%c\n", &sect_cat); 

這會吃換行符,而不是把它留在stdin

希望這會有所幫助!

+0

我寧願OP考慮使用'fgets'而不是'scanf',但任何試圖使用'fflush(stdin)'的人都有辦法去,所以我會解決。 – 2012-02-03 01:41:48

+3

它沒有按預期工作。輸入字母后,程序等待另一個輸入,然後繼續。 – 2012-02-03 02:08:48

+0

雖然'fflush(stdin)'(或任何閱讀流)未定義爲ISO C,POSIX確實定義了文件可查找的情況下的行爲。儘管對於不可觀察的流仍然未定義,並且「stdin」通常是不可檢測的(例如,如果它是終端或管道)。 – 2012-02-03 02:43:36

0

fflush應該與輸出流工作,看文檔here