gpt4 book ai didi

c - 如何对输入进行验证?数字或字母..C 编程

转载 作者:行者123 更新时间:2023-11-30 19:22:26 24 4
gpt4 key购买 nike

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;
}

关于c - 如何对输入进行验证?数字或字母..C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com