Expressions FAQ


Q: I'm getting a segmentation fault

95% of your errors in this lab are going to be from empty stacks. If a stack is empty and you try to use .pop() or .top() you will get a segmentation fault. This is (at the very least) a pretty simple fix. If you do run into that that segmentation fault, just have a check around the code that is giving you the error. For example if this line has an error:

if(stack.top() == "]") {}
Then surround it with a check of some kind to make sure that the stack is not empty:
if(!stack.empty()) {
    if(stack.top() == "]") {}
}
There are many different ways you can implement checks on empty stacks, and the above example might work well for some situations, and for other situations you might need to come up with another solution. The bottom line is to make sure that you never try to access anything on an empty stack, and you should be good!

Q: What is the difference between a parentheses mismatch and unbalanced parentheses in this lab?

In this lab, a parentheses mismatch occurs if you find a closing symbol that doesn't match the symbol on the top of the stack. This can also occur if you find a closing symbol and the stack is empty.

Unbalanced parentheses occur if you have processed every character from the string, and there are still opening symbols on the stack.

Q: How can I keep track of the expression? I get one expression but then I have to call multiple functions on it.

The easiest way to solve this, is to just have a private data member inside of your ExpressionManager class that keeps track of your expression. Then all of your functions have access to the current expression (infix, postfix, value, etc) and you can just override that variable when you get a new expression.