Beginner here. Wrote a program to check if entered alphabet is lowercase or upppercase. (Practising if/else questions) I thought of two methods:
- Range method-getting the desired result
- Union method-not getting the desired result
Here is the code,
if (enteredAlphabet>='a' && enteredAlphabet <='z')
{
cout<<enteredAlphabet<<" is lowercase.";
}
and
if (enteredAlphabet==('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z'))
{
cout<<enteredAlphabet<<" is lowercase.";
}
CodePudding user response:
The expression 'a'||'b'||...
is actually a short-hand for ('a' != 0) || ('b' != 0) || ...
.
The result of the expression will always be the boolean value true
.
Since true
can be converted to the integer value 1
your comparison is really the same as enteredAlphabet == 1
. This comparison will likely never be true.
What you actually should use is std::islower
:
if (std::islower(enteredAlphabet)) { /* Is lower case letter */ }