Uppercase or Lowercase

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?
@takaflaka

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;
}
Please use the <> button to the right of the text box to highlight code.

Switch-case statements do not work like that. http://en.cppreference.com/w/cpp/language/switch

There are pre-defined functions that can determine if a char is lowercase of uppercase:
http://www.cplusplus.com/reference/cctype/isupper/
http://www.cplusplus.com/reference/cctype/islower/

1
2
3
4
5
6
7
if(islower(ascii)) {
  cout << "The letter is lowercase." << endl;
} else if(isupper(ascii)) {
  cout << "The letter is uppercase." << endl;
} else {
  cout << "The number is invalid." << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;

int main()
{
	char num, store;
	int choice;
	cin >> num;
	cout << "\n1-Upper\n";
	cout << "\n2-Lower\n";
	cout << "\n Enter The Choice : ";
	cin >> choice;
	switch (choice)
	{
	case 1:
	{
		store = num - 32;
		cout << store;
		break;
	}
	case 2:
	{
		store = num + 32;
		cout << store;
		break;
	}
	default:
	{
		cout << "\nInvalid input";
	}
	}
	
	system("pause");

}
@bird1234
You're making an unnecessary assumption about the current locale. The standard library functions in <locale> do the same job, better.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace 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;
    else if( 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;
}
See the example at the bottom of the page, here
http://en.cppreference.com/w/cpp/locale/isupper
closed account (48T7M4Gy)
@bird1234 @takaflaka

A couple of points about your OP:

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.
Last edited on
takaflaka, in fact, you can use the standard library function: islower(char ch) and isupper(char ch) to solve the question.
Thank you guys! I appreciate the help. :D
@kemort

i was making my code simple , he is a beginner and i am pretty much sure he does not know what toupper and tolower do

thnx by the way

happy coding :)
closed account (48T7M4Gy)
@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 :(

Cheers :)
Topic archived. No new replies allowed.