2017-02-09 42 views
0

我寫了一些C代碼,你可以通過輸入1或2來從答案中選擇,如果你輸入一個更高的數字,它會帶你回來選擇另一個數字就好了。但是,如果我嘗試輸入不是值或數字的內容,如字符串或字符,底部的錯誤消息將無限重複。當輸入任何其他字符時,如何讓我的代碼的行爲與輸入比1或2更高的數字相同?這裏我使用抽象的代碼:如何停止製作此C代碼循環?

#include <stdio.h> 

int a; 

int main(){ 
    b:scanf("%d", &a); 
    if(a==1) 
    { 
     a=0; 
    } 
    if(a==2) 
    { 
     a=0; 
    } 
    else 
    { 
     a=0; 
     printf("\nERROR: Please try again.\n\n"); 
     goto b; 
    } 
} 

編輯:顯然返回值仍然卡在scanf()時,它返回到它。我如何清除scanf()的返回值?

+5

我會使用一個'while'循環這個,而不是goto方法的。然後只是從循環中斷開,或者設置一個標誌來決定是否繼續循環。 goto的合法使用案例遠遠少於合法用例。 – Carcigenicate

+3

呃...後藤。呸。學習正確的方法來做到這一點*現在*,在爲時已晚之前。 –

+2

如果你用'fgets'得到輸入,然後在輸入字符串中使用'sscanf',當用戶沒有輸入你需要的東西時,你可以忘記以前的輸入字符串並重試。 OTOH使用'scanf'時,任何被拒絕的輸入都將保留在輸入緩衝區中。 **總是**檢查'scanf'函數族的返回值(參見手冊頁瞭解其含義)。永遠不要認爲輸入數據是*好*。 –

回答

-2

類似...... 注:顯然999是一個任意值。只是選擇給你一個例子。

#include <stdio.h> 


int main(){ 
int a = 1; 
while (a != 999){ 
    scanf("%d", &a); 
    if(a==1) 
    { 
    a=0; 
    } 
    if(a==2) 
    { 
    a=0; 
    } 
    else if (a != 999) 
    { 
     a=0; 
     printf("\nERROR: Please try again.\n\n"); 
    } 
} // while() 
} // main() 
+3

定義了「b」在哪裏?這甚至不會編譯,我認爲我們不應該宣傳'goto'。 – stevieb

+0

您還需要檢查'scanf()'的返回值。 –

+0

放鬆。別緊張。這是因爲我複製了初始代碼並很快修改了它。 goto不見了。意味着從原始來源刪除第二次參考。 另外,從來沒有拋出異常,因爲這也是一個轉到。 :)想一想,拋出異常並且代碼直接跳轉到錯誤位置。呃哦,轉到。 :) – raddevus

-1
#include <stdio.h> 

int a; 

int isNumeric(const char *str) 
{ 
    while(*str != '\0') 
    { 
     if(*str < '0' || *str > '9') 
      return 0; 
     str++; 
    } 
    return 1; 
} 

int main(){ 

    char inputStr[10]; 
    while(1){ 
     scanf("%9s",inputStr); 
     if(!isNumeric(inputStr)){ 
      a=0; 
      printf("\nERROR Not a number: Please try again.\n\n"); 
     }else { 
      a = atoi(inputStr); 
      if(a==1){ 
       a = 0; 
      }else if(a == 2){ 
       a == 0; 
      }else{ 
       a=0; 
       printf("\nERROR : Please try again.\n\n"); 
      } 
     }`enter code here` 
    } 
} 

沒有測試。但我想你會得到一個好主意。檢查strtol函數。這也很有用。

+2

也許'scanf(「%9s」,inputStr)'也使用'isdigit' –

+1

...並測試scanf()的返回值以確保它有一個值。 –

+0

完成老闆:)。順便說一句,我猜isdigit需要循環相同的循環次數。所以我希望這不會是一個性能問題。乾杯:) – vk3105

-1

請勿使用gotos。取而代之,使用while循環:

#include <stdio.h> 

int main(void) { 
int a, end = 1; //end determines if the loop should end 
do { //a do-while loop - it's the same as a while loop, except it runs atleast once 
    scanf("%d", &a); 
    switch (a) { //switches the value of a 
    case 1: 
    case 2: printf("You entered %d\n", a); 
      end = 0; //sets end to 0, which will end the loop(see below) 
      break; 
    default: printf("\nERROR: Please try again.\n\n"); 
    } 
} while (end); //every non-zero value is true, so when I set end to 0, it will end the loop 
return 0; //don't forget the return 0: it shows you that your program ran without error 
} 

所以我寫到它只要輸入有效的輸入就會結束。您也不需要將a設置爲零,因爲每次運行循環時都會再次讀取它。
編輯:如果你想檢查是否有無效的輸入,如5x,您可以使用以下命令:

int check, var, error; 
char ch; 
do { 
error = 0; 
check = scanf("%d%c", &var, &ch); 
if (check != 2 || ch != '\n') { 
    printf("Wrong input. Try again -> "); 
    error = 1; 
    fflush(stdin); 
} 
} while (error); 
+1

也許檢查'scanf'的返回值將有助於 –

+0

'switch'在if語句會執行時過度 –