Here, I am trying to convert infix expression into postfix expression (I am also adding a ( at the start and ) at the end to make sure its a fully paranthesised equation)
The program works fine however, the operators when returned turns into a garbage value or something like that. I have failed to find the mistake I made in the program and need somebody's help to please provide it for me...
Here's the output that I am getting.
1 2 3 4
Enter Expression: (a+b)*c
abc
Press any key to continue . . .
However, it should be like this,
1 2 3 4
Enter Expression: (a+b)*c
ab+c*
Press any key to continue . . .
The issue is with the loop on line 100, it should be this:
1 2 3 4
while ((c = obj.pop()), c != '(')
{
Output[j++] = c;
}
The issue with your original loop is that you're checking if (c = obj.pop()) != '(' - which doesn't make sense. You want to set "c" THEN check if it's equal to something.
Even better, you could do this:
1 2 3 4 5
while (obj.peek() != '(')
{
c = obj.pop();
Output[j++] = c;
}
So there's less vagueness
EDIT:
Here's why the loop condition caused this issue:
while (c = obj.pop() != '(')
^That will NOT make c = obj.pop() THEN check if not-equal to '('. This code actually first will do the "!=" check THEN will assign "c" ... to a bool. Once "obj.pop() != '('" is evaluated to true or false, "c" will then become assigned "true" or "false" - outputting the symbol you were getting.