Hello. I'm trying to write a program (switch statement) that's able to print whether a user-inputted letter is uppercase or lowercase, but I'm not sure how to do that. I don't even know how to print lowercase or uppercase letters to begin with, could someone please explain this to me?
If the letter inputted was ascii number 65 thru 90, then an uppercase was entered, and if it's 96 thru 122, then it was lowercase. And lastly, if it doesn't fall in those limits, then it was a non-alphabetical input.
Am I doing this wrong? Please point out any mistakes, thank you.
#include <iostream>
using namespace std;
int main(){
char ascii;
cout<<"Enter a letter by using an ASCII number: ";
cin>>ascii;
switch(ascii){
case ((ascii>=65)&&(ascii<=90))
cout<<"The letter is lowercase."<<endl;
break;
case ((ascii>=96)&&(ascii<=122))
cout<<"The letter is uppercase."<<endl;
break;
case ((ascii>=0)&&(ascii<96)&&(ascii<90)&&(ascii>96)&&(ascii>122)&&(ascii<=128)
cout<<"This number is invalid."<<endl;
break;
}
cout<<endl;
system("pause");
return 0;
}
if(islower(ascii)) {
cout << "The letter is lowercase." << endl;
} elseif(isupper(ascii)) {
cout << "The letter is uppercase." << endl;
} else {
cout << "The number is invalid." << endl;
}
#include <iostream>
usingnamespace std;
int main()
{
int answer = 0;
char ascii;
cout<<"Enter a letter by using an ASCII number: ";
cin>>ascii;
if( ascii >= 'a' && ascii <= 'z')
answer = 0;
elseif( ascii >= 'A' && ascii <= 'Z')
answer = 1;
else
answer = 2;
switch( answer )
{
case 0:
cout << "The letter is lowercase."<<endl;
break;
case 1:
cout << "The letter is uppercase."<<endl;
break;
case 2:
cout<<"This character is invalid."<<endl;
break;
default:
cout << "Completely out of control - shouldn't ever get here." << endl;
}
return 0;
}
1. You were pretty close as you can see.
2. The switch required a clearer expression than you had. There may be ways to do it along the lines you had but the separate if ... else is simpler and easier to follow and maintain.
3. You'll notice the power of the char in the use of 'a' to 'z' 'A' to 'Z' etc ( even '0' to '9' f you wanted to include digits) which takes the cross referencing out of contention/confusion.
Cheers :)
PS I have no idea whether/how your program goes in Thailand or Greece. You can chase that one up.
@bird1234
My apologies, I just noticed I directed my comment to you by mistake. It wouldn't have made much sense to you and probably appeared as unsolicited criticism of your valuable contribution.
I meant to direct my tips @takaflaka and will change my post accordingly where they should make more sense.
You'll be pleased to know I always try to direct my comments to the OP rather than other posters but this time my blooper intervened :(