2015-09-26 98 views
0

我是新來C編程,我想檢查我的所有數組元素是除第一個元素以外的整數。無限循環時輸入無效輸入

我寫了下面的代碼,但是一旦插入錯誤的輸入,循環就不會停止。

bool validate_int(char input[]){ 
    fgets(input,10, stdin); 
    for(int i = 1; i < strlen(input); ++i) { 
     if(!isdigit(input[i])){ 
      i = 1; 
      fgets(input,10, stdin); 
     } 
     else{ 
     } 
    } 
    return true; 
} 
+3

你絕不能忽略的輸入操作的返回值。你的方法被打破,無法修復。 –

+0

@KerrekSB呃......只有在錯誤檢查是需求的情況下。我聽到人們說了很多,但嚴重的是,這可能是一個用於學習字符串操作的小型玩具程序,而不是用於瞭解處理I/O錯誤的學習。 – immibis

+0

我只想檢查從索引1開始的數組元素是不是其他類型的整數。 –

回答

2

您的代碼有一些小問題。這裏是一個更好的辦法來做到這一點(未經測試)

bool validate_int(char input[]) /* Bad function name; See @Filipe's comment */ 
{ 
    for(;;) /* Infinite loop */ 
    { 
     if(fgets(input, 10, stdin) == NULL) /* If fgets failed */ 
     { 
      puts("fgets failed"); 
      return false; 
     } 

     int i, len = strlen(input); 

     if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */ 
      input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */ 

     if(!isalpha(input[0])) /* If the first character of input is not an alphabet */ 
      continue; /* Loop again */ 

     if(len == 1) /* There is no number */ 
      continue; 

     for(i = 1; i < len; ++i) 
     { 
      if(!isdigit(input[i])) /* If not a digit */ 
       continue; /* Loop again */ 
     } 

     break; /* Get out of the loop */ 
    } 

    return true; 
} 

更更好的方式是獨立的輸入和驗證分爲兩個獨立的功能(未經測試)

bool getInput(char input[]) 
{ 
    if(fgets(input, 10, stdin) == NULL) /* If fgets failed */ 
    { 
     puts("fgets failed"); 
     return false; 
    } 

    int len = strlen(input); 

    if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */ 
     input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */ 

    return true; 
} 

bool validate(char input[]) 
{ 
    if(!isalpha(input[0])) /* If the first character of input is not an alphabet */ 
     return false; 

    int i, len = strlen(input); 

    if(len == 1) /* There is no number after the character */ 
     return false; 

    for(i = 1; i < len; ++i) 
    { 
     if(!isdigit(input[i])) /* If not a digit */ 
      return false; 
    } 

    return true; 
} 

和在調用功能(再次,未經測試)

char input[10]; 
if(getInput(input)) 
{ 
    if(validate(input)) 
    { 
     puts("Input is in correct format"); 
    } 
    else 
    { 
     puts("Input is in wrong format"); 
    } 
} 
else 
{ 
    puts("Failed to get input"); 
} 
+0

我想你多次調用'strlen'。我認爲''len'應該在調用'fgets'並檢查其返回值之後計算('int len = strlen(input);')。 i''len'應該用於檢查換行符,並且如果刪除換行符,應該調整(遞減)。 – MikeCAT

+0

謝謝。編輯。希望它看起來不錯。 –

+0

我們不應該向OP展示這樣做的好方法嗎?通過分隔驗證輸入? – Cubia

1

試試這個

bool validate_int(char input[]){ 
    bool valid; 

    do{ 
     valid = false; 
     fgets(input,10, stdin); 
     for(int i = 1; input[i] && input[i] != '\n'; ++i) { 
      if(!isdigit(input[i])){ 
       valid = false; 
       break; 
      } else { 
       valid = true; 
      } 
     } 
    }while(!valid); 
    return true; 
} 
+0

@感謝很多傢伙。 –

2

這裏是另一種方法,我會採取,這是必須清潔IMO:

這是你的驗證功能:

bool customValidation(char *string) 
{ 
    int len = strlen(string); 
    if (!isalpha(string[0]) || (len > 1 && !isdigit(string[1]))) 
     return false; 

    for (int i = 1; i < len && string[i] != '\n'; ++i) 
     if (!isdigit(string[i])) 
      return false; 

    return true; 
} 

這是你將如何使用它:

char input[10]; 
do 
{ 
    fgets(input, 10, stdin); 
} while (!customValidation(input)); 

顯然你應該重命名customValidation()爲更重要的東西。

+0

@CoolGuy自從我上次使用fgets()之後過了一段時間,我認爲我們需要添加一個額外的char來容納'\ 0'。編輯。 – Cubia

+0

我認爲當輸入有一個數字時,你的代碼不能正常工作,例如:'A \ n' –

+0

@CoolGuy增加了驗證,因此如果第一個位置沒有數字,它就會失敗。 – Cubia

-1

檢查:

int a_Length = 10; 
char input[a_Length]; 
fgets(input,a_Length, stdin); 
for(int i = 1; i < strlen(input); ++i) { 
    if(!isdigit(input[i])&& input[i]!='\n'){ 
     i = 0; 
     printf("Again try: "); 
     if (input[a_Length - 1 ]!='\n') 
      getchar(); 
     fgets(input,10, stdin); 
    } 
} 
+0

1)問題是關於C,而不是C++ 2)'fgets'不會在標準輸入流中保留換行符。它將它消耗並存儲在'input'中(假設'input'中有空格)。 –

+0

你能運行'fgets(input,10,stdin); char c = getchar();'?解釋爲什麼char c ='\ n'會得到?@ CoolGuy –

+0

什麼是輸入?它是否是9個字符,不包括'\ n'? –