isdigit() 问题; - scanf 验证器
Issue with isdigit(); - scanf validator
我要看C语言的字符串是全是字母还是全是数字
我的代码:
char Name[50];
int flag = 0;
int charString = 0;
printf("Please insert your name\n");
scanf("%[^ A-Z\n]",Name);
while (Name[charString]){
flag = isdigit(Name[charString]);
while(flag){
getchar();
printf("ERROR! Please insert only letters");
scanf("%[^ A-Z\n]",I.Nome);
}
charString++;
}
使用"[^ A-Z\n]"
排除大写字符,可以排除数字但做"[^ A-Z0-9\n]"
.
然后检查输入是否有效只需检查 scanf()
returns 1
if (scanf("%[^ A-Z0-9\n]", Name) != 1)
{
printf("Invalid Input!\n");
/* do something about it */
}
试试这个
#include <stdio.h>
int main(void){
char Name[50], nl=0;
int stat;
printf("Please insert your name\n");
stat=scanf("%49[a-z]%c", Name, &nl);
while(stat != 2 || nl != '\n'){
while(getchar() != '\n');//drop upto newline
printf("ERROR! Please insert only letters\n");
stat=scanf("%49[a-z]%c", Name, &nl);
}
printf("your name is '%s'\n", Name);
return 0 ;
}
如果您不想让用户用户输入数字,请禁用数字输入。
这将忽略除字母或 space 之外的所有键,甚至不显示它们。
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char name[50];
char key=0;
int i=0;
while(1)
{
key=getch(); //get input from keyboard
if(!key)
getch(); //if pressed key is a extended ASCII key ignore it
if(isalpha(key)||key==32)
putch(name[i++]=key); //if pressed key is a-z or A-Z or space, display it and add to name.
else if( key==8 && i>=0) //if backspace is pressed erase a character before it
{
printf("%c%c%c",8,32,8);
i--;
}
else if(key==13) //if enter key is pressed end the loop
break;
}
name[i]='[=10=]';//end of the string
}
我要看C语言的字符串是全是字母还是全是数字
我的代码:
char Name[50];
int flag = 0;
int charString = 0;
printf("Please insert your name\n");
scanf("%[^ A-Z\n]",Name);
while (Name[charString]){
flag = isdigit(Name[charString]);
while(flag){
getchar();
printf("ERROR! Please insert only letters");
scanf("%[^ A-Z\n]",I.Nome);
}
charString++;
}
使用"[^ A-Z\n]"
排除大写字符,可以排除数字但做"[^ A-Z0-9\n]"
.
然后检查输入是否有效只需检查 scanf()
returns 1
if (scanf("%[^ A-Z0-9\n]", Name) != 1)
{
printf("Invalid Input!\n");
/* do something about it */
}
试试这个
#include <stdio.h>
int main(void){
char Name[50], nl=0;
int stat;
printf("Please insert your name\n");
stat=scanf("%49[a-z]%c", Name, &nl);
while(stat != 2 || nl != '\n'){
while(getchar() != '\n');//drop upto newline
printf("ERROR! Please insert only letters\n");
stat=scanf("%49[a-z]%c", Name, &nl);
}
printf("your name is '%s'\n", Name);
return 0 ;
}
如果您不想让用户用户输入数字,请禁用数字输入。 这将忽略除字母或 space 之外的所有键,甚至不显示它们。
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char name[50];
char key=0;
int i=0;
while(1)
{
key=getch(); //get input from keyboard
if(!key)
getch(); //if pressed key is a extended ASCII key ignore it
if(isalpha(key)||key==32)
putch(name[i++]=key); //if pressed key is a-z or A-Z or space, display it and add to name.
else if( key==8 && i>=0) //if backspace is pressed erase a character before it
{
printf("%c%c%c",8,32,8);
i--;
}
else if(key==13) //if enter key is pressed end the loop
break;
}
name[i]='[=10=]';//end of the string
}