Click here to Skip to main content
14,930,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For some reason, this program runs but compiler is terminating and sends me to debug and termination.

Any idea why? I'm stuck like chuck.

I commented in the code what it's supposed to do. I can scrap this whole thing and just try something else, and trust me, I'm about to ha ha ha, but I really want to know why the compiler is puking it back out


C++
#include<iostream>
#include<conio.h>
#include<ctype.h>

using namespace std;

/*
	The program will evaluate a postfix expression that contains digits and operators.
	The program tries to simulate the microprocessor execution stack or evaluation
	of expression.
 */

//The class performing the evaluation
class Evaluation {
	public:
		int st[50];
		int top;
		char str[50];
		Evaluation() {
			top = -1;
		}

		//function to push the item
		void push(int item) {
			top++;
			st[top] = item;
		}

		//function to pop an item
		int pop() {
			int item = st[top];
			top--;
			return item;
		}

		//function to perform the operation depending on the operator.
		int operation(int a,int b,char opr) {
			switch(opr) {
				case '+':return a+b;
				case '-':return a-b;
				case '*':return a*b;
				case '/':return a/b;
				default: return 0;
			}
		}

		int calculatePostfix();
};

//This is the function that calculates the result of postfix expression.
int Evaluation::calculatePostfix() {
	int index = 0;
	while(str[index]!='#') {
		if(isdigit(str[index])) {
			push(str[index]-'0');
		}
		else {
			int x = pop();
			int y = pop();
			int result = operation(x,y,str[index]);
			push(result);
		}
		index++;
	}
	return pop();
}

/*
	main function that reads the postfix expression and	that prints the result.

	The input expression should be ending with a number
	An example input expression would be:
	123*+
	Its output will be 7.

*/
int main() {
	void clrscr();
	Evaluation eval;
	cout << "Enter the postfix: ";
	cin >> eval.str;
	int result = eval.calculatePostfix();
	cout << "the result is " << result;
	getch();
}
Posted
Updated 15-Sep-20 2:36am
Comments
   
"...compiler is terminating and sends me to debug and termination..." I cannot believe that. Maybe, it's your code is terminating, not a compiler? Then run it under debugger.
—SA
lotussilva 2-Jul-13 11:40am
   
I don't know how to debug in Code::Blocks
joshrduncan2012 2-Jul-13 13:36pm
   
Please reply to the above post, otherwise SA will never know you left a message for him.

Just out of curiosity, what IDE are you using?
lotussilva 3-Jul-13 9:59am
   
Code::Blocks
JackDingler 2-Jul-13 15:11pm
   
A fundamental skill of development is being proficient in using a debugger.

Those pop commands are suspect BTW. What happens when you call pop and you have no data?
What happens when you call pop() and top is -1?
lotussilva 3-Jul-13 10:00am
   
Then you go on stack under flow!

You need to suffix your input string with #.

This line;
C++
while(str[index]!='#') {

means it will loop over str until it finds a #, if that is not present in the string then it will fail.

Try inputting this;
123*+#


Hope this helps,
Fredrik
   
Comments
nv3 2-Jul-13 17:18pm
   
That is very likely the problem. My 5.

And in addition, I would recommend to check for reaching the string end in form of the NULL-character; just in case the user forgets to put the # at the end. Actually, this terminating # is a bad idea anyhow. Just use the string end to detect the end of the expression. I hope OP reads my hint. :-)
Fredrik Bornander 2-Jul-13 17:42pm
   
Thank you.
I agree with you on the parsing comment, the whole part that reads and parses the string is a bit dodgy to say the least.
lotussilva 3-Jul-13 10:12am
   
Thank you! That worked. This is a tutorial from a book I was given and tutorials sometimes have you do unnecessary stuff you wouldn't normally encounter in the real world, only cause they want to teach you something.

I am still a little unsure with the whole postfix expresion thing. I don't know if I understand it well or what it does, or how this program is related to it for that matter :(
Fredrik Bornander 3-Jul-13 10:21am
   
Glad I could help.

Postfix expression means push the arguments first, then when an operator is found execute it on the operands on the stack. In your example;
<pre>
Step 1: Push 1
Step 2: Push 2
Step 3: Push 3
Step 4: Operator*, pop 3 and 2 and multiply them into 6, push 6
Step 5: Operator+, pop 6 and 1 and add them into 7, push 7
</pre>
lotussilva 3-Jul-13 10:25am
   
oooooooh! That makes sense now! This is why I always come here when I'm stuck. Thanks again
Fredrik Bornander 3-Jul-13 10:26am
   
Glad I could help again :)
You can edit this line in your code to avoid the termination
C++
while(str[index]!='\0')
   
v2
Comments
Dave Kreskowiak 31-Oct-19 14:34pm
   
I seriously doubt the OP is still looking for a solution SIX YEARS LATER.
There are some mistakes in this
First one is while(str[index]!='#')

Change '#' into '\0'

The second one is operation function, In the code given above you are performing operations between first popped and second popped element but you need to perform operation between second popped and first popped(To be presice According to given solution if 43- is postfix expression operation is done like 3-4=-1 but correct one is 4-3=1)
so change a little here :
else {
int x = pop();
int y = pop();
int result = operation(x,y,str[index]);
push(result);
}
Do a small change as operation(y,x,str[index])

Example 4325*-+
According to code we get 11 as answer I think but correct answer is -3(I have checked it in internet)
We will get that answer if we make the changes suggested by me....

The total code is as follows:

#include<iostream>
using namespace std;
class Evaluation {
	public:
		int st[50];
		int top;
		char str[50];
		Evaluation() {
			top = -1;
		}
		void push(int val) {
			top++;
			st[top] = val;
		}
		int pop() {
			int val = st[top];
			top--;
			return val;
		}
		int operation(int a,int b,char opr) {
			switch(opr) {
				case '+':return a+b;
				case '-':return a-b;
				case '*':return a*b;
				case '/':return a/b;
				default: return 0;
			}
		}
		int calculatePostfix();
};
int Evaluation::calculatePostfix() {
	int index = 0;
	while(str[index]!='\0') {
		if(isdigit(str[index])) {
			push(str[index]-'0');
		}
		else {
			int x = pop();
			int y = pop();
			int result = operation(y,x,str[index]);
			push(result);
		}
		index++;
	}
	return pop();
}
int main() {
	Evaluation eval;
	cout << "Enter the postfix: ";
	cin >> eval.str;
	int result = eval.calculatePostfix();
	cout << "The result is " << result;
	return 0;
}

P.S.Please correct me if I am wrong
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900