|
-
September 26th, 2011, 04:02 PM
#4
Re: really need your help
Code:
#include <iostream>
int main(int argc,char * argv[])
{
char chInput = 0;
cin << chInput;
if ( (chInput >= 'A' && chInput <= 'Z') || (chInput >= 'a' && chInput <= 'z') )
{
std::cout << "Your input is a Character.";
}
else if ( chInput >= '0' && chInput <= '9' )
{
std::cout << "Your input is an Integer.";
}
else
{
std::cout << "Invalid input.";
}
return 0;
}
To explain that code example to a beginner at C++, a character holds ASCII text values. In the ASCII code table the characters 'A' and 'Z' are arranged in order from A to Z. Same goes for '0' to '9'. So you just compare the character input against the literal character values.
If input is greater than or equal to 'A' and less than or equal to 'Z', or if input is greater than or equal to 'a' and less than equal to 'z', then it's a character.
If the input is greater than or equal to '0' and less than or equal to '9', then it is an integer.
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
|