Please help me know the problem with my code and help me correct it

#include
using namespace std;
int main(){
char ch;
ch=cin.get();
long long int a,b,c;
while(true){
if(ch == β€˜+’){
cin>>a>>b;
c = a + b ;
cout<<c<<endl;
}
else if(ch == β€˜-’ ){
cin>>a>>b;
c = a - b ;
cout<<c<<endl;
}
else if(ch==’*’){
cin>>a>>b;
c = a * b;
cout<<c<<endl;
}
else if(ch==’/’){
cin>>a>>b;
c = a / b ;
cout<<c<<endl;
}
else if(ch==’%’){
cin>>a>>b;
c = a % b ;
cout<<c<<endl;
}
else if(ch==β€˜X’ or ch==β€˜x’){
break;
}
else{
cout<<β€œInvalid operation.Try again.”<<endl;
}
ch=cin.get();

}
return 0;

}

@shresth_2000, there are some mistakes in your code ::

  1. you are not taking input for ch (operator) inside your while loop.
  2. the condition in while loop should be (ch!=β€˜X’ and ch!=β€˜x’) , in your case,i.e, (ch!=β€˜X’ or ch!=β€˜x’) it will result true for every case as if ch==β€˜X’ in that case ch!=β€˜x’ will be true and loop will execute and vice versa
  3. cout<<β€œInvalid operation.Try again.”<<endl; there is a space between full stop and Try

corrected code :- https://ide.codingblocks.com/s/292921
In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED in case you got your answer

sir why should it be (ch!=β€˜X’ and ch!=β€˜x’) instead of (ch!=β€˜X’ or ch!=β€˜x’) as it is clearly mentioned in the program to terminate the code on encountering a char β€˜X’ or char β€˜x’

the reason is , or returns false in only one case i.e false | false so for (ch!=β€˜X’ or ch!=β€˜x’) to give false ch!=β€˜X’ has to be false as well as ch!!=β€˜x’ has to be false which is not possible in any situation as if ch==β€˜X’ then ch!=β€˜x’ will be true, and if ch==β€˜x’ then ch!=β€˜X’ will be true, so your while loop will run forever

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.