Check uppercase or lowercase

closed account (4jzvC542)
The question given is :

Write a program that will ask user to enter a character. Check if it is alphabetic or not? If alphabetic, Check weather it is Uppercase or Lowercase.


The code I have written is this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  //check_alphabet.cpp
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
	char ch;
	bool x;
	cout << "Enter a Character : ";
	cin >> ch;
	x = isdigit(ch);
	if( x == true)
		cout << "Not a Alphabet. ";
		//break;
	else
		x = isalpha(ch);
		if( x == true)
			cout << ch << " is Uppercase. " << endl;
		else
			cout << ch << " is Lowercase. " << endl;
	return 0;
}


The problem is that for any value be it digit, any letter it show Uppercase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
//#include <ctype.h>
#include <cctype>

//using namespace std;

int main()
{
    char ch;
    //bool x;
    std::cout << "Enter a Character : ";
    std::cin >> ch;

    // x = isdigit(ch);
    if( std::isalpha(ch) ) // if it is alphabetic
    {
        if( std::isupper(ch) ) std::cout << ch << " is uppercase.\n" ;
        else std::cout << ch << " is lowercase.\n" ;
    }

    else std::cout << ch << " is not alphabetic.\n" ;
}
Your program in proper formatting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//check_alphabet.cpp
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
    char ch;
    bool x;
    cout << "Enter a Character : ";
    cin >> ch;
    x = isdigit(ch);
    if( x == true)
        cout << "Not a Alphabet. ";
    //break;
    else
        x = isalpha(ch);
    if( x == true)
        cout << ch << " is Uppercase. " << endl;
    else
        cout << ch << " is Lowercase. " << endl;
    return 0;
}
Do you the problem now?
You do check first whether a char is a digit or not. The assignment expects you to test for alpha or not.

Then you do check whether the char is alpha or not. You were supposed to test whether an alpha (and only alpha) is upper or not.

[Edit] Totally missed that bad scoping.
Last edited on
closed account (4jzvC542)
woohooo I messed up badly.....
yes yes I got my fault
thanks guys you are really lifesaver.....

@JLBorges Thanks very very much.... your code is much much better(of course)

@MiiNiPaa errr......... the format for code i use is ANSI foospace... I compiled the code as it is in your formt but error is elseware

@keskiverto yes... my mistake I mis interpreted it

Thank You all
Topic archived. No new replies allowed.