+ 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; }
7 Réponses
+ 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".
+ 6
Thank you very much shreyash joshi .
+ 5
Thank you Théophile , the code has been worked.
+ 4
Don't use || or operator but && and operator. Because all small letters are <='Z'
+ 3
Y should use && (and) instead of || (or) cuz both conditions must be true.
+ 2
You're welcome!
+ 1
Ur's welcome!!!