Hello adam2016,
Below is Bjarnes's code with a few additions to pause the program before the window closes, at least the way I use it the window closes before I can read what was output, anyway it will give you a chance to see what is output.
I do not think you are understanding how "cin" works. From the keyboard an entry of "2 +2 * 3 x" is stored in the input buffer first. This is why the program works the way it does because it is getting the input to the variables from the buffer not directly from the keyboard.
Now "cin >> lval;" is known as formatted input. Because "lval" is defined as an "int" the input is expecting a number only. If anything other than a number is entered the stream fails and one or more of the state bits of the stream is set. That is where the first
if (!cin)
comes into use. This happens when you enter something other than a number for "lval". The second comes into use when it receives something other than a number for "rval".
The way the program works is when you enter "2 + 2 * 3 x" from the keyboard this is stored in the input buffer. "cin >> lval" will extract from the buffer the first number up to the first white space and stop. Then in the while condition "cin >> op" extracts the (+, -, * or /) from the buffer up to the next white space then "cin >> rval" extracts up to the next white space and so on until the "\n" is reached leaving the "\n" in the input buffer. This way you keep extracting from the input buffer until "e" or "x" is extracted and the program ends with the "return 0;".
The program works and any length of expression will work, as long as you follow the correct format, until a "e" or "x" is read from the buffer.
Revised code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <chrono>
#include <thread>
//using namespace std;
int main()
{
std::cout << "please enter an expression ( +,-,*,/) " << std::endl;
std::cout << "add an x to end of expression to end it" << std::endl;
int lval = 0;
int rval;
char op;
std::cin >> lval;
if (!std::cin)
{
std::cout << "error no first operand" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
return 1;
}
while (std::cin >> op)
{
if (op != 'x')
{
std::cin >> rval;
}
if (!std::cin)
{
std::cout << "no second operand" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
return 1;
}
switch (op)
{
case '+':
lval += rval;
break;
case '-':
lval -= rval;
break;
case '*':
lval *= rval;
break;
case '/':
lval /= rval;
break;
default:
std::cout << "result = " << lval << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
return 0;
}
} //end while
std::cout << " error :: bad expression" << std::endl;
return 0;
}
|
If anything is still unclear let me know.
Hope that helps,
Andy