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.
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