Basic Calculator

What’s wrong with this code. It is giving wrong answer in 3 test cases out of 6.

#include
using namespace std;
int main() {
int n1,n2,o;
char ch;

while(1)
{
    cin>>ch;
    
    if(ch=='x'||ch=='X')
     break;
switch(ch)
{
    case'+':
     {
         cin>>n1>>n2;
         o=n1+n2;
         cout<<o<<endl;
         break;

     }
     case'-':
     {
         cin>>n1>>n2;
         o=n1-n2;
         cout<<o<<endl;
         break;
     }
     case'*':
     {
         cin>>n1>>n2;
         o=n1*n2;
         cout<<o<<endl;
         break;
     }
     case'/':
     {
         cin>>n1>>n2;
         o=n1/n2;
         cout<<o<<endl;
         break;
     }
     case'%':
     {
         cin>>n1>>n2;
         o=n1%n2;
         cout<<o<<endl;
         break;
     }
     default:
     {
         cout<<"Invalid operation. Try again.";
         break;
     }
}
}
return 0;

}

link to the above code

Hello @hrithik.anand02,

There is a single mistake in your code:
You have to change line after print “Invalid operation. Try again.”

Modified Code:

Hope, this would help.
Give a like if you are satisfied.

Thank you so much for helping

Anytime :blush:

Please, mark this doubt as resolved if you haven’t.