+ 6

What's the wrong with this program?

I want to code a program with c++ that distinguish between capital letters and small letters. The problem is that when I type any letter, the output is Capital. This is the code: #include <iostream> using namespace std; int main() { char x; cin>>x; if (x>='A' || x<='Z') { cout<<"This letter is capital."; } else if (x>='a'|| x<='z') { cout<<"This letter is small."; } else { cout<<"This isn't a letter."; } return 0; }

5th Jan 2019, 10:40 AM
Janna Al-Ward
Janna Al-Ward - avatar
7 odpowiedzi
+ 8
The || stands for "or" which means that even one condition in your statement is true the whole statement becomes true. So if i give input as "a"( small letter "a"),then whole if statement becomes true and if block gets executed. This is because it will compare on basis of askii value , the askii value of "a" is 97 which is greater than A( askii value=65). Use the && operator instead of || operator in your code for correct functioning. "&&" Stands for logical "and".
5th Jan 2019, 11:02 AM
shreyash joshi
shreyash joshi - avatar
+ 6
Thank you very much shreyash joshi .
5th Jan 2019, 11:16 AM
Janna Al-Ward
Janna Al-Ward - avatar
+ 5
Thank you Théophile , the code has been worked.
5th Jan 2019, 10:55 AM
Janna Al-Ward
Janna Al-Ward - avatar
+ 4
Don't use || or operator but && and operator. Because all small letters are <='Z'
5th Jan 2019, 10:48 AM
Théophile
Théophile - avatar
+ 3
Y should use && (and) instead of || (or) cuz both conditions must be true.
5th Jan 2019, 4:48 PM
John Ds
John Ds - avatar
+ 2
You're welcome!
5th Jan 2019, 10:57 AM
Théophile
Théophile - avatar
+ 1
Ur's welcome!!!
5th Jan 2019, 11:47 AM
shreyash joshi
shreyash joshi - avatar