|
-
September 24th, 2011, 05:50 AM
#1
really need your help
I just started to study the C++ Programming Language. I'm done with the condition statements and jump statements. An idea came to my mind to create a simple program
that will determine if the input is a character or integer.
For example, the program asks you "How old are you?" and then you input a character (A-Z), the program will output "Your input is a Character."
Same as, if the program asks you "Choose a letter from A-Z" and then you input a integer (0-9), the program will output "Your input is a Integer."
Your reply will be a really big help to me as I study C++.
Thanks..
-
September 24th, 2011, 06:54 AM
#2
Re: really need your help
-
September 26th, 2011, 03:59 AM
#3
Re: really need your help
[ removed duplicate thread ]
Please do not create multiple threads about the same issue.
And please try to give a meaningful title to your threads.
-
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.
-
September 26th, 2011, 04:28 PM
#5
Re: really need your help
 Originally Posted by CppCoder2010
To explain that code example to a beginner at C++,
http://www.codeguru.com/forum/showpo...18&postcount=2
Stick to explaining without the code. We don't do homework for others here.
Regards,
Paul McKenzie
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
|