So I am learning C++ and am working on a couple of challenges. The purpose of the code below is to create a stack, and then use it to convert infix to postfix. Currently anything without parenthesis will be converted no problem, but I cannot get it to work with parenthesis. I am going to post the whole code, but my guess is the problem lies in the toPostFix function, the else if block dealing with the right parenthesis. Thanks a ton for the help I have been smashing my face on the wall for about 4 hours now.
At line 78 you are popping off the stack and adding to the output until you have an operator on the top with a greater priority than the current infix char. Since you are entering that section when you have a +-*/^ infix char that always have non-zero priority you will always pop off any ('s on the stack since ('s return zero if you call priority() with them.
It is obvious you are trying to code Dijkstra's Shunting-yard algorithm, I believe there may be a deeper problem here. A few days ago I coded the same algorithm and I don't have the code with me, but I don't remember guarding against adding a ( to the output. So I think once you fix that you may have a more subtle bug somewhere. I also had left/right associativity to check in addition to priority which I don't see anywhere here. You should read the Wikipedia article on the Shunting-yard algorithm, they have an example that made coding it quite straightforward. I used the STL <stack> than my own homegrown stack and a map<char,pair<int,int>> to store priority/associativity rather than use a function to return the values.
If you don't have a satisfactory answer by tomorrow I can post my solution when I get to work, its on my work computer.
This will work with all single digit operands 0-9, the operators +-*/^, and will do braces correctly. Gives undefined results for malformed expressions like mismatched brackets, numerous operators in a row, etc. Its basically the most simplistic implementation imaginable. But it display the central idea i was driving at: