Stack Calculator Error
Hello, I am having a problem with the attached code.
The problem is with the main loop; the calculator works once for any expression, but when it is looped back, there is a segmentation fault.
I think it might have something do to with the input stream, but I am unable to fix it.
Any help would be greatly appreciated!!!!
ok the first thing I would do is put in dummy cout statements within the function I think the error occurs, until I narrow it down to the problem statement.
For example put a
and the beginning of that function, and another reading "returning" or something at the end. You'll very quickly find the problem yourself.
cout << "Entered read_and_evaluate." << endl;
edit: I know it's frustrating having an error you can't find, but I think when you eventually discover and fix it, it's like a mini-high, for me at least for me.
I already know when the error occurs - the error is when it tries to pop something from the stack when there is nothing there.
And this happens because when it tests the while loop in the read and evaluate function, it returns false the second time around.
Anyone have any idea on how I can fix this?
You have twos problems. The first is that when you read choice in main() it leaves a newline in the input buffer so when your while loop tests if peek() returns '\n' it is, so the loop is not executed. The second part of the problem is that you never clear the newline after reading in the equation so that helps foul up the read of choice.
Try (cin >> choice).get();
and an ins.get(); at the end of your function.
Wow, thank you so much.
I never would have figured that one out.