what is the problem with this code
some testcases are giving wrong answer
Basic calculator code
#include
using namespace std;
int main() {
while(1)
{
char ch;
cin>>ch;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
{
long long int n1,n2,n3;
cin>>n1>>n2;
if(ch=='+')
n3=n1+n2;
else if(ch=='-')
n3=n1-n2;
else if(ch=='*')
n3 = n1*n2;
else if(ch=='/')
{
if(n2==0)
break;
else
n3 = n1/n2;
}
else if(ch=='%')
{
if(n2==0)
break;
else
n3 = n1%n2;
}
cout<<n3<<endl;
}
else if(ch=='X'||ch=='x')
break;
else
{
cout<<"Invalid operation. Try again."<<endl;
continue;
}
}
return 0;
}
You must be thinking, why am i sending the same code to you.
yes , it is the same code but with a minute modification.
moreover, this code is passing all the test cases.
Can you find the difference?
cout<<“Invalid operation. Try again.”<<endl;
please, read the question properly before attempting it.
You have applied the correct logic but the way you are producing output is incorrect.
don’t worry, these are the silly mistakes everyone does. Practice is the only solution to eliminate them.
Keep practicing
hope this would help.
if you still have doubts, feel free to ask.
mark the doubt as resolved if you are satisfied with my answer.
if possible, give a like and review about me.
thankyou
sir this code is still not passing some test cases
Can you run the code I have modified.
Let me know, if it is not passing the test cases.
Actually, I don’t have my lapi with me right now n it very difficult to handle the ide on mobile.
If it runs well, check for the difference.
Else, I’ll resolve your doubt before 11 am.
sir it is not working
I’ll manage a lapi and will resolve it by today. Apologies for the delay.
N m sorry, I acknowledged ur other post too.
again, i repeat. Please, read the question properly.
There is a set format of outputs in question.
so, follow them
btw your mistake is a full stop “.”
you are printing:
“Invalid operation. Try again”
and you are required to print:
“Invalid operation. Try again.”
Here’s your solution.
Hope, this would help.
oh okay thanks