Need help please!!! postfixEvaluate!!HELP

I'm running this part in a test driver and not getting the right values.
SO,i know i have to create an if statement saying... if the first thing in my string is an operator..return invalid...
idk how to write that in code?? please help!!
Testing Part 4


postfixEvaluate() returned incorrect response for: + 3 4 - 1
Student: 1
Expected: invalid
postfixEvaluate() returned incorrect response for: - 1
Student: 1
Expected: invalid
postfixEvaluate() returned incorrect response for: + 1 3 4 -
Student: -1
Expected: invalid
3+ Test's Failed.

Proceeding to next Test.
Part 4 Failed


1+ TESTS FAILED


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
string postfixEvaluate(string postfixExpression){

		stack<int> mystack;

		string next_character;
		string tempOperators;
		int tempTop;
		int tempSecond;
		int finalresult;
		int tempString;
		stringstream ssp;
		ssp<<postfixExpression;
		while(!ssp.eof()){


			ssp>>next_character;

			if(isNumber(next_character)){ /*if the character is a number*/
				int integer = atoi(next_character.c_str());
				mystack.push(integer);

			}
			else if(isOperator(next_character)){

				if(mystack.size()>1){
					if (next_character == "+"){
						tempTop = mystack.top();
						mystack.pop();

						tempSecond = mystack.top();
						mystack.pop();

						tempString = tempSecond + tempTop;
						mystack.push(tempString);
					}
					if (next_character == "-"){
						tempTop = mystack.top();
						mystack.pop();

						tempSecond = mystack.top();
						mystack.pop();

						tempString = tempSecond - tempTop;
						mystack.push(tempString);
					}
					if (next_character == "*"){
						tempTop = mystack.top();
						mystack.pop();

						tempSecond = mystack.top();
						mystack.pop();

						tempString = tempSecond * tempTop;
						mystack.push(tempString);
					}
					if (next_character == "/"){
						tempTop = mystack.top();
						mystack.pop();

						tempSecond = mystack.top();
						mystack.pop();

						tempString = tempSecond / tempTop;
						mystack.push(tempString);
					}
					if (next_character == "%"){
						tempTop = mystack.top();
						mystack.pop();

						tempSecond = mystack.top();
						mystack.pop();

						tempString = tempSecond % tempTop;
						mystack.push(tempString);
					}
				}
			}/*end of else if*/
			//else if (isOperator)
			else
				return "invalid";



		}

		finalresult = mystack.top();
		mystack.pop();
		stringstream conversion;
		conversion << finalresult;
		if(mystack.size() > 1){
					return "invalid";
				}


		return conversion.str();
	}
Last edited on
This code wouldn't even compile, so I'm having a hard time seeing how it is being tested. If I assumed that commenting out line 78 was the correct thing to do to get it to compile, would I be correct?

What happens when line 25 evaluates to false?
yes, you were right.. i just fixed.. it compiles..
what do you mean? could you be more specific please?
claudilla wrote:
what do you mean? could you be more specific please?

Presumably that was in response to:
What happens when line 25 evaluates to false?


And what I meant was: What happens when line 25 evaluates to false? What should happen when line 25 evaluates to false? The two things are not the same.

Line 34 is also suspect. Often looping on eof is not the correct thing to do.
Last edited on
Topic archived. No new replies allowed.