Guess what, if I let the both variables be op1, then if calculate: why that when i add 3 3 + ; no problem encounter it gives 6.Code:#include <iostream> #include <stack> void calculation(int, int, char); using namespace std; stack<int> a; void main(void) { bool expression = false; char ch; char done; int op1; cout << "Reverse Polish Notation : " << endl; cout << "Enter expression: " << endl; while (!expression) { cin >> op1; cin >> op1; cin >> ch; calculation(op1, op1, ch); if (!cin) expression = false; else { expression = true; //cin >> op1; //cin >> op2; //cin >> ch; //calculation(op1, op2, ch); } } cout << "Your expression is " << a.top() << endl; system ("pause"); } void calculation(int oper1, int oper2, char chr) { switch (chr) { case '+': a.push(oper1 + oper2); break; case '-': a.push(oper1 - oper2); break; case '*': a.push(oper1 * oper2); break; case '/': a.push(oper1 / oper2); break; } }
When i add 2 3 +, it gives 6 instead of 5, i dun understand the logic and error.
When i add 1 2+, it gives 4.