whats still wrong with this code as its not working…?
Basic calculator
Try using long long int instead of int
The program takes inputs for two numbers only when ch is among ‘+’, ‘-’, ‘*’, ‘/’ or ‘%’. But you are taking the inputs without checking for ch.
Also when ch is ‘X’ or ‘x’, the program should terminate, which means return 0; statement. The break statement just breaks out of the switch case and not the outer while loop.
i didnt understood yours ‘ch’ concept
Its not a concept. It is mentioned in the question only.
i m still not getting the ans.
Send your modified code’s link please
Read the question carefully first. You have done the same mistake again.
When ch is ‘X’ , ‘x’ or any other invalid symbol, you don’t have to take inputs for the two numbers.
So check for ch first without taking any integer inputs. And if ch is among ‘+’, ‘-’, ‘*’, ‘/’ or ‘%’, then only take the two integer inputs.
still i m not getting correct testcases.
A break statement in switch just breaks out of the switch loop and it does not break out of the while loop.
I modified your code. Check it out.
what is wrong in my code please tell
one test case is successful but 3 are wrong please tell what is wrong in this code
#include
using namespace std;
int main()
{
long long int n,m;
char ch;
do
{ cin>>ch;
if(ch==‘X’ || ch==‘x’)
{break;}
switch(ch)
{
case ‘+’: {cin>>n>>m;
if(n<=100000000 && n>=0 && m>=0 && m<=100000000)
{cout<<n+m<<endl;} break;}
case ‘-’: {cin>>n>>m;if(n<=100000000 && n>=0 && m>=0 && m<=100000000)
{cout<<(n-m)<<endl;} break;}
case '’: {cin>>n>>m;
if(n<=100000000 && n>=0 && m>=0 && m<=100000000)
{cout<<nm<<endl;} break;}
case ‘/’: {cin>>n>>m;
if(n<=100000000 && n>=0 && m>0 && m<=100000000)
{cout<<(n/m)<<endl;}
break;}
case ‘%’: {cin>>n>>m;
if(n<=100000000 && n>=0 && m>0 && m<=100000000)
{cout<<(n%m);}
break;}
default :{cout<<“Invalid operation. Try again.”;
break;}
}
}while(1);
return 0; }