2016-12-03 349 views
-1

我需要檢查輸入是否爲數字。我的代碼看起來是這樣的:檢查輸入是否爲數字C

int input; 

while (scanf(" %s %d", string, &input) != EOF) { 

if (isNotANumber(input)) { 
    printf("Not a number"); } 

doSomethingElse(input, string); 
} 

編輯:我需要接受輸入和調用函數doSomethingElse(輸入),直到用戶輸入EOF。 isNotANumber是一個模擬功能,我沒有那個功能,我在問我怎麼寫它。

編輯2:變量字符串需要是一個字符串,變量輸入需要是一個整數。

編輯3:我試着分開我的代碼到這一點:

while (scanf(" %s", string) != EOF) { 
    if (scanf("%d",&input) != 1) { 
    printf("not a number"); 
} 
doSomething(); 
} 

但還是老樣子並不像「4A」輸入工作。

+1

我認爲'isNotANumber(輸入)'不能在循環檢查'input' 。 – BLUEPIXY

+0

由於'input'被定義爲'int',所以它的值將永遠是一個整數,至少與'scanf()'沒有失敗一樣長。 – alk

+0

如果您不知道輸入是數字,則不能使用scanf。 (或者,你可以使用scanf,但是如果scanf沒有讀取輸入,因爲它不是一個數字,那麼你將不得不以其他方式讀取它,所以scanf毫無意義。)需要以其他方式輸入數據。嘗試'fread',然後使用'strtol'檢查它是否是一個整數。順便說一句,你的意思是「數字」,還是「整數」?準確。 –

回答

1

例如,你可以改變它如下。

#include <stdio.h> 

#define doSomethingElse(input) do{ printf("your input is %d\n", input); }while(0) 

int main(void){ 
    int input; 
    int status; 

    while ((status = scanf("%d", &input)) != EOF) { 
     if (status == 0) { 
      printf("Not a number\n"); 
      while(getchar() != '\n'); //clear input 
     } 
     else { 
      doSomethingElse(input); 
     } 
    } 
} 

但是,這不能檢查輸入如123.456。 (接受123
所以,建議用fgets輸入並用strtol查詢。


前面已經指出的那樣,像scanf(" %s %d", string, &input)數量後不能檢查輸入。

所以,爲了方便,請檢查後向輸入。

char string[32], ch; 
int input; 
int status; 


while ((status = scanf("%31s %d%c", string, &input, &ch)) != EOF) { 
    if (status == 3 && ch == '\n') { 
     doSomethingElse(input); 
    } 
    else { 
     printf("Not a number\n"); 
     while(getchar() != '\n'); //clear input 
    } 
} 

使用 fgetsstrtol

實施例(mystrtoi了重組chux的answer。感謝)

#include <stdio.h> 
#include <errno.h> 
#include <limits.h> 
#include <stdlib.h> 

#define doSomethingElse(input) do{ printf("your input is %d\n", input); }while(0) 

int mystrtoi(const char *str, int *err) { 
    char *endptr; 
    *err = errno = 0; 
    long l = strtol(str, &endptr, 0); 
    if (errno == ERANGE || *endptr != '\0' || str == endptr) { 
     *err = 1; 
    } 
    // Only needed if sizeof(int) < sizeof(long) 
    if (l < INT_MIN || l > INT_MAX) { 
     *err = 1; 
    } 
    return (int) l; 
} 


int main(void){ 
    char line[128]; 
    char string1[32], string2[128]; 
    int num, err; 

    while (fgets(line, sizeof line, stdin)){ 
//  if(2 != sscanf(line, "%31s %31s", string1, string2)){// or use strtok to split 
     if(2 != sscanf(line, "%31s %127[^\n]", string1, string2)){ 
      printf("invalid input\n"); 
      continue; 
     } 
     num = mystrtoi(string2, &err); 
     if(err) { 
      printf("Not a number\n"); 
     } 
     else { 
      doSomethingElse(num); 
     } 
    } 
} 
+0

謝謝。我不得不編輯代碼,我現在如何檢查我的輸入? – Kate

+0

@Kate情況變化不大。如果'status == 1'輸入失敗。 – BLUEPIXY

+0

它仍然接受輸入爲「字符串4a」,也應該被視爲「不是數字」。我該如何解決這個問題? – Kate

0
​​

scanf代碼有兩個問題:

  • scanf返回成功讀取的項目,而不是EOF數量。在這裏,您要檢查是否scanf已成功讀取一個整數input
  • scanf預期的變量

你應該重新寫行地址:

while (scanf("%d", &input) == 1) 
+0

我編輯了我的帖子。你能再看一遍嗎? – Kate

+2

如果你的'scanf'成功了,那麼'input'已經是一個數字 – artm