Your program needs to be able to distinguish between numbers, such as
and operators such as
One way to do this is to read the input one character at a time
|
char ch = std::cin.get();
|
and then test it using either a series of if/else statements, or using a switch-case.
Follow a set of rules:
ch is a newline '\n'
That's the end of the input. Stop processing.
ch is a space or tab '\t'
ignore it and proceed
ch is one of '+', '-', '/', '*'
Store the operator in a character variable.
ch is a digit from '0' to '9'
Put the character back into the stream, then read the number.
1 2
|
std::cin.putback(ch);
std::cin >> number;
|
and after reading the number, decide what to do with it.
If there is not any operator already stored, store the number in the total.
if there is an operator already stored, carry out the operation with the stored total and the current number, and save the result. example, operator is '+'
After end of input, print out the total.