|
-
Jan 11th, 2002, 08:29 PM
#1
Thread Starter
Fanatic Member
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?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 12th, 2002, 05:38 AM
#2
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;
}
-
Jan 12th, 2002, 06:38 AM
#3
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);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 12th, 2002, 12:21 PM
#4
Thread Starter
Fanatic Member
Ok, thanks for your help guys
Alcohol & calculus don't mix.
Never drink & derive.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|