set1:
printf("Name : ");
gets (name);
if (isalpha(name)) {printf("\nSorry, input is invalid\n");
goto set1;}
这是一块的我的代码,以及i声明的名称为char名称[30]; 但它表示* char类型的错误参数与参数类型int不兼容,以及如果我们一起输入随机字母和数字(例如gghjhj88888)如何验证?如何对输入进行验证?号或字母.. C编程
谢谢你的帮助?
set1:
printf("Name : ");
gets (name);
if (isalpha(name)) {printf("\nSorry, input is invalid\n");
goto set1;}
这是一块的我的代码,以及i声明的名称为char名称[30]; 但它表示* char类型的错误参数与参数类型int不兼容,以及如果我们一起输入随机字母和数字(例如gghjhj88888)如何验证?如何对输入进行验证?号或字母.. C编程
谢谢你的帮助?
isalpha
预计int
不是char *
(指针)。您应该遍历字符串并单独验证字符:
for(int i = 0; i < strlen(name); i++){
if(!isalpha(name[i])){
/* errors here */
}
}
另外:goto's are bad!。所以是gets
,改用fgets
。
检查的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;
}
#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;
}
谢谢@BLUEPIXY :)有效 – 2013-05-09 22:53:33
不客气。 – BLUEPIXY 2013-05-09 23:00:09
后一个完整的测试用例。如何声明'stdNumb'?它是如何初始化的? ... – LihO 2013-05-09 21:01:40
你应该看看while循环 – 2013-05-09 21:02:19
我认为你需要一些涵盖基础知识的好书。然后你需要开始阅读你使用的函数的文档:[isalpha](http://linux.die.net/man/3/isalpha) – LihO 2013-05-09 21:04:08