I'm working on a calculator program. It is suppose to accumulate the operations performed. Acceptable characters are +, -, *, %, /, =, and !
Where I run into problems is printing the results when the characters input are = or !.
The following is my code (less the statement for = & ! (I keep getting an endless loop).
I'm still learning, so bear with me for any mistakes within the code.
Any help would be appreciated. Thank you!
Code:
#include <iostream.h>
int result; // the result of the calculations
char oper_char; // the user-specified operator
int value; // value specified after the operator
main()
{
result = 0; // initialize the result
// loop forever (or until break reached)
while (1) {
cout << "Result: " << result << '\n';
cout << "Enter operator and number: ";
cin >> oper_char;
if (oper_char == '!')
break;
cin >> value;
if (oper_char == '+') {
result += value;
} else if (oper_char == '-') {
result -= value;
} else if (oper_char == '*') {
result *= value;
} else if (oper_char == '%') {
result %= value;
} else if (oper_char == '/') {
if (value == 0) {
cout << "Error: Divide by zero\n";
cout << " operation ignored\n";
} else
result /= value;
} else {
cout << "Unknown operator " << oper_char << '\n';
}
}
return (0);
}