Basic calculator Test Cases fail

Please check the following code and tell me what am I doing wrong.

Hey @jaisalsinghani

  1. you can’t write return(main()), this is not correct

  2. also, you need to use a while loop as you have to input characters and numbers multiple times.

  3. use a break statement after the code of every case of the switch, as it is in the syntax of switch case

  4. use flag, whenever x or X occurs, we’ll set the flag to 1 and this will be a way to break the loop.
    inside the main function, you’ll have to write the following loop-
    int flag = 0;
    while(true){

    cin>>ch;
    switch (ch)
    {
    case ‘+’ :
    cin>>n1>>n2;
    op=n1+n2;
    cout<<op<<endl;
    break;

     case '-':
     cin>>n1>>n2;
     op=n1-n2;
     cout<<op<<endl;
     break;
    
     case '*':
     cin>>n1>>n2;
     op=n1*n2;
     cout<<op<<endl;
     break;
    
     case '/':
     cin>>n1>>n2;
     op=n1/n2;
     cout<<op<<endl;
     break;
    
     case '%':
     cin>>n1>>n2;
     op=n1%n2;
     cout<<op<<endl;
     break;
    
     case 'X' : flag = 1;
         break;
     case 'x' : flag = 1;
         break;
     default :
     cout<<"Invalid operation. Try again"<<endl;
     break;    
    

    }
    if(flag == 1) break;
    }

Thanks a lot for the help!!

1 Like