more C++ programming guidance
I am working on example7-6 from O'Reilly's Practical C++ Programming book. I am finding that I need to enter 'q' twice to quit the program, but the code says to me that I should only need to enter 'q' once. any clues what is going on here? I want the code to be written in a way that I only need to enter 'q' once.
Thank you everybody for your assistance in advance.
-- program output ---
% ./calc3 Result: 0
Enter operator and number: q
q
%
--- snip ----
Code:
#include <iostream>
int result; // the result of the calculations
char oper_char; // operator the user specified
int value; // value specified after the operator
int main()
{
result = 0; // initialize the result
// loop forever (or until break reached)
while (true) {
std::cout << "Result: " << result << '\n';
std::cout << "Enter operator and number: ";
std::cin >> oper_char >> value;
if ((oper_char == 'q') || (oper_char == 'Q'))
break;
if (oper_char == '+') {
result += value;
} else if (oper_char == '-') {
result -= value;
} else if (oper_char == '*') {
result *= value;
} else if (oper_char == '/') {
if (value == 0) {
std::cout << "Error:Divide by zero\n";
std::cout << " operation ignored\n";
} else
result /= value;
} else {
std::cout << "Unknown operator " << oper_char << '\n';
}
}
return (0);
}