postfix expression help!

Im supposed to write a postfix expression evaluator with the menu i made and it isn't accepting expressions it keeps crashing please help!

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<iostream>
#include<string>
#include<stack>
#include<iomanip>

using namespace std;


int postfixEval(string s)
{
		stack<int> myStack;
		int ch,result,a,b;
		char op;

		for(int count = 0; count < s.size(); count++)
		{
			ch = s[count];
			if(isdigit(s[count]) ) //check if operand
				myStack.push(int(ch)-48); //push the value of the operand ch
			else
			{
				ch=op; //other wise ch is an operator named op

					switch(op)
					{

						case '+':
							if(myStack.size() <2)
							{
								cout << "Incorrect syntax!\n";
								return 0;
							}
							b = myStack.top();
							myStack.pop();
							a = myStack.top();
							myStack.pop();
							myStack.push(a + b);
							break;
                            //pop both, add, push

						case '-':
							if(myStack.size() <2)
							{
								cout << "incorrect syntax!\n";
								return 0;
							}
							b = myStack.top();
							myStack.pop();
							a = myStack.top();
							myStack.pop();
							myStack.push(a - b);
							break;
							//pop both, subtract, push


						case '/':
							if(myStack.size() <2)
							{
								cout << "incorrect syntax!\n";
								return 0;
							}
							b = myStack.top();
							myStack.pop();
							a = myStack.top();
							myStack.pop();
							myStack.push(a / b);
							break;
							//pop both, divide, push



							case '*':
							if(myStack.size() <2)
							{
								cout << "incorrect syntax!\n";
								return 0;
							}
							b = myStack.top();
							myStack.pop();
							a = myStack.top();
							myStack.pop();
							myStack.push(a * b);
							break;
							//pop both, multiply, push

						default :
							cout<<"incorrect input\n";

				}
			}

		}
		result = myStack.top();
	return result;

}
int main()
{
	int input;
	bool quit = false;
	string s = " ";

	do {

		cout<<"1 Evaluate postfix expression\n";
		cout<<"2 Quit Program\n";
		cin>>input;

		switch (input)
		{
			case 1:
				cout << "Postfix expression: ";
				cin.clear();
				cin >> s;
				cout<< s << " = " << postfixEval(s) <<"\n";
				break;
			case 2:
				quit = true;
				break;
			default:
				cout<<"Please make a valid selection!\n";
				break;
		}
	} while (!quit);

	return 0;
}
closed account (o3hC5Di1)
Hi there,

Could you please be a bit more specific as to "crashing"?
When does the program crash, immediately or after a certain action?
Do you get any error messages?

The more specific you are, the better we'll be able to help you.

All the best,
NwN
I don't really know anything about std::stack but looking at the first lines of your code shows some error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int postfixEval(string s)
{
    int ch, ....
    char op;

    for ( int count = 0; count < s.size(); count++ ) {
        ch = s[count]; // your trying to assign a char to an int

        //...

         if ( ... ) {
         }
         else {
            ch = op; //  ch is an int, assigned to the undefined value of op, i think you mean the inverse of this
                switch ( op ) { ... }
         }
   }
}


and what if s[0] ... is neither an operand or operator ? what should the program do ?
Last edited on
Topic archived. No new replies allowed.