Simple calculator

what is the problem in the code ? i am not getting desired output.

hi @Raghavsingla522

this is because you have taken input only once
and after that same ch will continue and
your code falls in infinite loop

but i have put that input step in while so it should repeat after every step. so what is wrong in that ?

cin>>ch;
while(ch==’+’||ch==’-’||ch==’*’||ch==’/’||ch==’%’)
{ int N1,N2;
cin>>N1>>N2;
switch(ch)
{
case β€˜+’
............

you have take input only once before while loop
if ch=’+’
it remain + and also go inside loop in this way it is going into infinite loop

modified code

it is showing no output bro .
check this out .
i have rectified that infinite loop error but now it is showing only output one time of first case only .

this will not work
because second time it is taking β€˜\n’ (enter) which you press while giving input

you have to use two cin.get()
but now it will run only two cases

you have to use
while(cin>>ch)
as i done in modified code
go through it

okay now it is giving only one problem ,i.e, it is not showing invalid output for other characters.

remove this
if(ch==’+’||ch==’-’||ch==’*’||ch==’/’||ch==’%’)
as we have default statement in switch

now the code is working all right according to sample output but still it is giving many test cases as wrong answer .


only one test case is running .

when you got x or X you have to return 0; terminate the program
break will just bring you out of switch statement

in default cout<<β€œInvalid operation. Try again.\n”;
this is correct

modified code

finally run successfully . thanks !

1 Like