BASIC CALCULATOR problem of

why “Invalid operation. Try again”. this prints infinite times
https://ide.codingblocks.com/s/43992

Hey Mayur, first check if the input character(x) is valid operator then only take the input numbers a and b. otherwise, if x is not a valid operator and you take input a and b as numbers may be you will take “X” or “x” also as a number and the loop will become infinite.

But as a and b is a no. And x is ‘X’ so why loop become infinite as using break statement it should not be infinite as i use condition on x so why value of a and b change output

Hey Mayur, in your code firstly you are taking the input ch, a and b, after that you are checking if x is a valid operator or not. Let’s dry run your code for this example:
input :
"
;
X

for this input your output should be
Invalid operation. Try again
Invalid operation. Try again

but your code will run infinitely as your code will take input like this
ch = "
a = ;
b = X

and then check if char ch is a valid operator or not, now as ch is not a valid operator it prints “Invalid operation. Try again” and the loop will run again as there is no more input left then cin will start taking garbage input and this loop will never break and become an infinite loop.

So, to correct this error first check if the input char ch is a valid operator or not, if it is a valid operator then only input a and b as numbers else print “Invalid operation. Try again” and run the loop again and take input char ch.

Hope it helps you :slight_smile: