you type 2+2q
it does some things and ends up in the + part of the switch statement.
at this point, cout of val is zero, then you add 2 to it, then it hits q, and it exits, answer = 2.
so the root problem is that val = 0 when it should be assigned 2 somewhere, somehow.
to see this, add print statements before and after the val += statement.