|
-
December 13th, 2003, 11:33 PM
#1
Member
DataType checking C++
I was wondering if there was any library function in C++ that allowed me to uncover if a user's input into a certain program is a number or a character. Usually when I have to deal with this kind of thing I just play around with ascii codes, but it would be pretty convenient to have a function check if the input is a letter or a number, no matter what I declared it as in my implementation.
I Speak in frequencies even dogs have trouble hearing
-
December 14th, 2003, 04:42 AM
#2
One of the major problems in C/C++ is that there is no datatype validation. However, if you want to validate a variable based on a datatype, you can use the sizeof() keyword in C or just overload the functions in C++. Another caveat with overloading, the default type for floating point literals is a double and NOT a float. So, if your user is going to be entering a float, you're going to have to have the parameter as a double in your overloaded function.
Cheers,
cgkanchi
EDIT: I didn't read your post properly. The following functions are provided in C/C++ standard libraries, all in <ctype.h>
isalpha(int i) ----> Checks to see if the char is an alphabet
islower(int i) ----> Checks to see if the char is a lowercase alphabet
isupper(int i) ----> Checks to see if the char is an uppercase alphabet
isalnum(int i) ----> Checks to see if the char is alphanumeric
ispunct(int i) ----> Checks to see if the char is a punctuation character (.,?, etc)
isdigit(int i) ----> Checks to see if the char is a decimal digit
isxdigit(int i) ----> Checks to see if the char is a hexadecimal digit (0-9,A-F,a-f)
iscntrl(int i) ----> Checks to see if the char is a control character (For example ASCII 13 is a control character)
isgraph(int i) ----> Checks to see if the char is any printable character except a space
isprint(int i) ----> Checks to see if the char is any printable character including a space
EDIT 2: In all these cases, the int is a variable that should be represantable as an unsigned char. You could (should) also pass a char/unsigned char as an argument as C/C++ do automatic type conversion.
Cheers,
cgkanchi
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
|
|