Floating point exception and infinite loop.

So for class we are working in Linux and we are supposed to use a class stack that we have created to perform PostFix Evaluations. Afterwards we should print what is left on the stack in the output file. To read in data we are using a Scanner coded by our professor. Everything is compiling fine but when I run the program I get a Floating Point exception. I tried inserting my Print function in the loop to check what is on the stack and when that happens I get a never ending trail of 1s and have to shut down the program manually.

I suspect there is something wrong with both the Print function and my Manager function, and that the Print function just delays the program from reaching the floating point exception. However I still don't understand what could be causing the exception in the first place.


The PostFixMgr Function:
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

void postFixMgr(FILE * i, ostream& o)
{
	Stack S;
	Scan L(i);					//Direct the Scanner to read from FILE i
	
	for(;;)
	{
		int t=L.Lex();
		if(t == 0) break;
		int x, y;
		switch(t)
		{
			case PLUS: 			//pop top two, add, push sum
				S.Push(S.Pop()+S.Pop());
				break;
			case MINUS:			//pop subtrahand, pop minuend, find diff, push diff
				y = S.Pop();
				x = S.Pop();
				S.Push(x-y);
				break;
			case STAR:	
				S.Push(S.Pop()*S.Pop());
				break;
			case SLASH:
				x = S.Pop();
				y = S.Pop();
				S.Push(y/x);
				break;
			case INTLIT:		//push numeric Value
				S.Push(t);
				break;
			default: 	throw BOpException();
		}
        //This is The line point where Print causes infinite loop.
        //If commented out I receive floating point exception
        S.Print(cout);
	}
	S.Print(o);
}



The Print Function in class Stack
1
2
3
4
5
6
7
8
9
10
11
12
void Stack::Print(ostream& o)
{
	Element* e;
	while(e)
	{
		e = tos;
		Element* p = e;
		e = e -> prev;
		o << p->value <<" "<< endl;
	}
	o << endl;
}


tos is a private member of stack pointing to the top of stack.
Element is a struct with an int value and an Element pointer prev

Any insight is greatly appreciated.
Floating point exception is usually an error you get when dividing by zero. Make sure x is not zero on line 28.

In the Print function you use e before you have assigned it a value.
Topic archived. No new replies allowed.