I have a program that executes basic aritmetic operations.
First the operator is inputed, then the two numbers of the operation.
The problem is, if I input, for example, "plus"
or other string as the Oper
character, for example:
plus 4 10
instead of
+ 4 10
it enters an infinite loop, and doesn't reset or stop for new inputs. Where does it go wrong?
Here is the code:
#include <iostream>
using namespace std;
void calc()
{
char Oper;
int num1, num2;
cout << "Enter operator and two numbers: ";
cin >> Oper >> num1 >> num2;
if (Oper == '+')
cout << num1 + num2 << endl;
else if (Oper == '/')
cout << num1 / num2 << endl;
else if (Oper == '*')
cout << num1 * num2 << endl;
else if (Oper == '-')
cout << num1 - num2 << endl;
else
calc();
}
int main()
{
while (true)
{
calc();
}
}