0

Okay, so I'm basically trying to create a C++ program that determines the case of the letter. The program will basically ask for a letter input, uppercase or lowercase and then the program will say whether that letter the user inputted is an uppercase or a lowercase letter. Help is advised. Thank you.


Input:

#include <iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter any character, lowercase or uppercase: ";
    cin >> ch;
    
    if ('A' != 'z')
    cout << "The character just entered is a lowercase letter." << endl;
    else if('a' != 'Z')
    cout << "The characer just entered is not a lowercase letter." << endl;
    else
    cout << "An invalid character was entered." << endl;
    
    system ("PAUSE");
    
    return EXIT_SUCCESS;
    return 0;
}

Output: LOWERCASE WORKS FINE
Enter any character, lowercase or uppercase: a
The character just entered is a lowercase letter.
Press any key to continue . . .

Output: UPPERCASE DOESN'T WORK
Enter any character, lowercase or uppercase: A
The character just entered is a lowercase letter.
Press any key to continue . . .


_______________________________________________
If I switch

if ('A' != 'z')

To instead of (!=) to (<), the program will flip over saying this.

Output: LOWERCASE DOESN'T WORK
Enter any character, lowercase or uppercase: a
The character just entered is not a lowercase letter.
Press any key to continue . . .

Output: UPPERCASE WORKS FINE
Enter any character, lowercase or uppercase: A
The character just entered is not a lowercase letter.
Press any key to continue . . .
_______________________________________________________
The exact same results appear if I switch this

else if('a' != 'Z')

Thank you for helping. I'm just a beginner in C++

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Kanoisa
0

I think that you have mis-understood what is going on in your if statements.

The condition if ('A' != 'z') is ALWAYS true for the standard ASCII character set. It is the same as writing if (65 != 122) which you can see is ALWAYS true.

Ok so what do you need to do. You need to test a range. Lets us first consider an integer value e.g.

int x=28;
if (x>10 && x<30)
  std::cout<<"X is between 10 and 30"<<std::endl;

As you can see I have two conditional tests combined with a boolean operator.
The if line first tests x>10 and then x<30 and combines them with an operator.

[NOTE: in C++ the compiler guarantees that it will test the first test first, and
that if it finds that the result is true/false without needing the remainder of the tests then it does not carry out those tests]

The && operator combines to results using the AND boolean operator.

For your test you can do this: if (ch>='a' && ch<='z') which tests to see if ch is between the ascii values of a and z.
Note that I have used a >= operator since obviously 'a' is a lower case value as is
'z'.

You could also use islower(ch) which is slightly better style because it deals with international character sets etc.

3

From here we have an example of the "correct" way to handle locale facets, including upper and lower.

// ctype::is example
#include <iostream>
#include <locale>
using namespace std;

int main ()
{
  locale loc;
  char quote[] = "It is wonderful how much may be done if we are always doing.";
               // (Attributed to Thomas Jefferson)
  int length = sizeof(quote);
  ctype<char>::mask * masks = new ctype<char>::mask [length];

  cout << '"' << quote << '"' << endl;

  cout << "The quote begins with an uppercase letter? ";
  bool upper = use_facet< ctype<char> >(loc).is( ctype<char>::upper, quote[0]);
  cout << boolalpha << upper << endl;

  int cx = 0;
  use_facet< ctype<char> >(loc).is (quote, quote+length, masks);
  for (int i=0; i<length; ++i) if (masks[i] & ctype<char>::space) ++cx;
  cout << "The quote has " << cx << " whitespaces.\n";
  return 0;
}
// output
// "It is wonderful how much may be done if we are always doing."
// The quote begins with an uppercase letter? true
// The quote has 12 whitespaces.
Votes + Comments
True, but take the OP's experience into account. He wouldn't know what to do with this code.
How it should be done
Nice example. Thx.
-1

Hey

For this problem i have devised a simple solution that i think meets your needs please consider the following.

#include <iostream>
#include <cctype>>

using namespace std;

int main()
{
    char ch;
    char compare;
    
    cout << "Enter any character, lowercase or uppercase: ";
    ch = cin.get();
    
    if(isalpha(ch))//check the character is a character not a number or escape sequence etc
    {
		compare  = toupper(ch);//convert ch to upperacse and store in compare (ch unchanged)
		if(compare==ch)//the original is upperace
		{
			cout<<"UpperCase character entered"<<endl;
		}
		else//character is lower case
		{
			cout<<"LowerCase character entered"<<endl;
		}
	}
	else
	{
		cout<<"Invalid Input "<<ch<<" is not a character"<<endl;
	}



    system ("PAUSE");

    return EXIT_SUCCESS;
    return 0;
}

Hope this helps, any questions about how my solution works if you cant see how it is working please post a reply or feel free to pm me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.