I'm trying to make a switch case calculator that will ask for an expression (e.g. 2+2) and print the answer, repeating the process until the user enters 'q'.
I can't figure out how to get the program to end when the user enters 'q'. the program below successfully asks for an expression, gives the answer, and asks for another. However when you enter an incorrect expression, it repeats the default case forever, including when you input 'q'.
I know the problem is to do with how I cin the variables, considering the operands are of type double and also something is wrong with the while loop, but I cant think of any alternatives, and can't seem to find a solution elsewhere.
int main() {
double operand1;
double operand2;
char operation;
double answer;
while (operation != 'q'){
cout << "Enter an expression:""\n";
cin >> operand1 >> operation >> operand2;
switch(operation)
{
case '+':
answer = (operand1 + operand2);
break;
case '-':
answer = (operand1 - operand2);
break;
case '*':
answer = (operand1 * operand2);
break;
case '/':
answer = (operand1 / operand2);
break;
default:
cout << "Not an operation :/";
return 0;
}
cout <<operand1<<operation<<operand2<< "=" << answer<< endl;
}
return 0;
}