set1:
printf("Name            : ");
gets (name);
if (isalpha(name)) {printf("\nSorry, input is invalid\n");
goto set1;}
这是我的一段代码,我将 name 声明为 char name [30];但是,它说 *char 类型的错误参数与参数类型 int.. 不兼容,以及如何验证我们是否一起随机输入字母和数字(例如 gghjhj88888)?
感谢您的帮助?
set1:
printf("Name            : ");
gets (name);
if (isalpha(name)) {printf("\nSorry, input is invalid\n");
goto set1;}
这是我的一段代码,我将 name 声明为 char name [30];但是,它说 *char 类型的错误参数与参数类型 int.. 不兼容,以及如何验证我们是否一起随机输入字母和数字(例如 gghjhj88888)?
感谢您的帮助?
#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;
}
检查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;
}