-
Type checking in C
If I ask the user for a string using gets(), how can I see if they entered a character instead of a number? I read somewhere that using gets() instead of scanf() was safer because strings can be anything and you need to check what they entered afterward, but how do I check?
-
ctype.h has a number of macros
isalpha() returns true if the char is an alphabetic character.
There are a bunch of related macros all starting with "is".
Goto Visual Studio help
[code]
// checks if a string is all alphabetic
int checkalpha(char *s){
int i;
int result;
result = TRUE;
i=0;
while(i<strlen(s) && result) result=isalpha(s[i++]);
return result;
}
-
gets is unsafe because it doesn't let you specify the number of characters you want to read at max. Use fgets instead:
Code:
char str[100];
fgets(str, 99, stdin);
-
Ok, thanks for your help guys :cool: