2013-05-09 36 views
-1
set1: 
printf("Name   : "); 
gets (name); 
if (isalpha(name)) {printf("\nSorry, input is invalid\n"); 
goto set1;} 

這是一塊的我的代碼,以及i聲明的名稱爲char名稱[30]; 但它表示* char類型的錯誤參數與參數類型int不兼容,以及如果我們一起輸入隨機字母和數字(例如gghjhj88888)如何驗證?如何對輸入進行驗證?號或字母.. C編程

謝謝你的幫助?

+1

後一個完整的測試用例。如何聲明'stdNumb'?它是如何初始化的? ... – LihO 2013-05-09 21:01:40

+0

你應該看看while循環 – 2013-05-09 21:02:19

+1

我認爲你需要一些涵蓋基礎知識的好書。然後你需要開始閱讀你使用的函數的文檔:[isalpha](http://linux.die.net/man/3/isalpha) – LihO 2013-05-09 21:04:08

回答

0

isalpha預計int不是char *(指針)。您應該遍歷字符串並單獨驗證字符:

for(int i = 0; i < strlen(name); i++){ 
    if(!isalpha(name[i])){ 
     /* errors here */ 
    } 
} 

另外:goto's are bad!。所以是gets,改用fgets

0

檢查的isalpha。其預計int作爲參數手冊頁。

要知道用戶的輸入是否有效的名稱或沒有,創建自己的功能,

/* a-z and A-Z are valid chars */ 
int isValidName(char *str) 
{ 
    if(str == NULL) 
    { 
    return 0; 
    } 

    while(*str) 
    { 
    if(! isalpha(*str)) 
    { 
     return 0; 
    } 
    str++; 
    } 
    return 1; 
} 
1
#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

int isdigits(char *s){ 
    //return value : true if the string is all numbers. 
    while(*s) 
     if(!isdigit(*s++)) 
      return 0; 
    return 1; 
} 

int main(void){ 
    char dateOfBirth[7]; 
    int len; 
set4: 
    printf("Date of Birth (DDMMYY) : "); 
    //Doesn't accept input more than specified number of characters 
    fgets(dateOfBirth, sizeof(dateOfBirth), stdin); 
    rewind(stdin);//keyborad buffer flush 
    //fflush(stdin);//discard the character exceeding the amount of input 
    //How fflush will work for stdin by the processing system (that is undefined) 
    //while ('\n' != fgetc(stdin));//skip if over inputted 
    len = strlen(dateOfBirth); 
    if(dateOfBirth[len-1] == '\n') dateOfBirth[--len] = '\0';//newline drop 
    if(len != 6 || !isdigits(dateOfBirth)){ 
     printf("\nSorry, input is invalid\n"); 
     goto set4; 
    } 

    return 0; 
} 
+0

謝謝@BLUEPIXY :)有效 – 2013-05-09 22:53:33

+0

不客氣。 – BLUEPIXY 2013-05-09 23:00:09