Need help.
Have the user enter a character. Tell them if the character is a
number, a lower case letter, an upper case letter or neither a letter nor a
number.
i cannot figure out how to make it so the program knows its neither a letter nor a number , this is my code so far
#include <iostream.h>
int main()
{
char a;
cout<<"enter a character"<<endl;
cin>>a;
if (a>=48)
{
if (a<=57)
cout<<"you have entered a number"<<endl;
}
if (a>=97)
{
if (a<=122)
cout<<"you have entered a letter"<<endl;
}
if (a>=65)
{
if (a<=90)
cout<<"you have entered an upper case letter"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
[995 byte] By [
k0b381] at [2007-11-20 11:42:06]

# 1 Re: Need help.
i think i solved it myself...
//Dennis Lam
#include <iostream.h>
int main()
{
char a;
cout<<"enter a character"<<endl;
cin>>a;
if (a>=48)
{
if (a<=57)
cout<<"you have entered a number"<<endl;
}
if (a>=97)
{
if (a<=122)
cout<<"you have entered a lower case letter"<<endl;
}
if (a>=65)
{
if (a<=90)
cout<<"you have entered an upper case letter"<<endl;
}
if (a>=0)
{
if (a<=47)
cout<<"not a number or a letter"<<endl;
}
if (a>=58)
{
if (a<=64)
cout<<"not a number or a letter"<<endl;
}
if (a>=91)
{
if (a<=96)
cout<<"not a number or a letter"<<endl;
}
if (a>=123)
{
if (a<=127)
cout<<"not a number or a letter"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
# 2 Re: Need help.
Hi all.
Take a look at isdigit, isupper, islower functions.
If you use these library functions you'll save time and work (unless you're doing an exercise or a homework).
# 3 Re: Need help.
i think i solved it myself...
//Dennis Lam
#include <iostream.h>
Incorrect/non-standard header. The correct header is <iostream>, not <iostream.h>.
If you're using a book that has <iostream.h>, the book is outdated or is not very good. The standard headers are the only ones you should be using.
Second, there are functions that you should use when determining whether a character is a digit, lower case, etc. Even if this is a homework exercise, you never determine if a character is lower case, digit, etc. by using "magic" numbers, as your code does.
As davide++ pointed out, the <cctype> functions such as isdigit(), islower(), etc. are to be used. The reason why is that your program cannot run under a non-ASCII collating sequence. There is still EBCDIC out there, where 'a' is *not* a 97, '0'is *not* 48, etc.
So if you were to hand in the assignment as you've written it, a knowledgable teacher would not give you full credit, as it shows these very bad flaws.
Regards,
Paul McKenzie