title: Convert from an infix expression to postfix (C++) using Stacks
body: <p>My lecturer gave me an assignment to create a program to convert and infix expression to postfix using Stacks. I've made the stack classes and some functions to read the infix expression. </p>
  
  <p>But this one function, called <code>convertToPostfix(char * const inFix, char * const postFix)</code> which is responsible to convert the inFix expression in the array inFix to the post fix expression in the array postFix using stacks, is not doing what it suppose to do. Can you guys help me out and tell me what I'm doing wrong?</p>
  
  <p>The following is code where the functions to convert from inFix to postFix is and <code>convertToPostfix(char * const inFix, char * const postFix)</code> is what I need help fixing:</p>
  
  <pre><code> void ArithmeticExpression::inputAndConvertToPostfix()
      {
         char inputChar; //declaring inputChar
         int i = 0; //inizalize i to 0
  
         cout &lt;&lt; "Enter the Arithmetic Expression(No Spaces): ";
  
         while( ( inputChar = static_cast&lt;char&gt;( cin.get() ) ) != '\n' )
         {
            if (i &gt;= MAXSIZE) break; //exits program if i is greater than or equal to 100
  
            if(isdigit(inputChar) || isOperator(inputChar))
            {
               inFix[i] = inputChar; //copies each char to inFix array
               cout &lt;&lt; inFix[i] &lt;&lt; endl;
            }
            else
               cout &lt;&lt; "You entered an invalid Arithmetic Expression\n\n" ;
  
            }
  
            // increment i;
            i++;
            convertToPostfix(inFix, postFix);
  
  
         }
  
  
  
  
      bool ArithmeticExpression::isOperator(char currentChar)
      {
  
          if(currentChar == '+')
              return true;
          else if(currentChar == '-')
              return true;
          else if(currentChar == '*')
              return true;
          else if(currentChar == '/')
              return true;
          else if(currentChar == '^')
              return true;
          else if(currentChar == '%')
              return true;
          else
              return false;
      }
  
      bool ArithmeticExpression::precedence(char operator1, char operator2)
      {
          if ( operator1 == '^' )
             return true;
          else if ( operator2 == '^' )
             return false;
          else if ( operator1 == '*' || operator1 == '/' )
             return true;
          else if ( operator1 == '+' || operator1 == '-' )
             if ( operator2 == '*' || operator2 == '/' )
                return false;
             else
                return true;
  
          return false;
      }
  
     void ArithmeticExpression::convertToPostfix(char * const inFix, char * const postFix)
          {
             Stack2&lt;char&gt; stack;
  
             const char lp = '(';
  
             stack.push(lp); //Push a left parenthesis ‘(‘ onto the stack.
  
             strcat(inFix,")");//Appends a right parenthesis ‘)’ to the end of infix.
  
            // int i = 0;
             int j = 0;
  
             if(!stack.isEmpty())
             {
  
                 for(int i = 0;i &lt; 100;){
  
                     if(isdigit(inFix[i]))
                     {
                          postFix[j] = inFix[i];
                          cout &lt;&lt; "This is Post Fix for the first If: " &lt;&lt; postFix[j] &lt;&lt; endl;
                          i++;
                          j++;
                     }
  
                      if(inFix[i] == '(')
                     {
                         stack.push(inFix[i]);
                         cout &lt;&lt; "The InFix was a (" &lt;&lt; endl;
                         i++;
                         //j++;
                     }
  
                      if(isOperator(inFix[i]))
                                 {
                              char operator1 = inFix[i];
  
                              cout &lt;&lt; "CUrrent inFix is a operator" &lt;&lt; endl;
                                     if(isOperator(stack.getTopPtr()-&gt;getData()))
                                         {
                                         cout &lt;&lt; "The stack top ptr is a operator1" &lt;&lt; endl;
                                         char operator2 = stack.getTopPtr()-&gt;getData();
                                             if(precedence(operator1,operator2))
                                             {
                                                 //if(isOperator(stack.getTopPtr()-&gt;getData())){
                                                     cout &lt;&lt; "The stack top ptr is a operato2" &lt;&lt; endl;
                                                     postFix[j] = stack.pop();
                                                     cout &lt;&lt; "this is post fix " &lt;&lt; postFix[j] &lt;&lt; endl;
                                                     i++;
                                                     j++;
                                                // }
  
                                             }
  
                                         }
                                     else
  
                                         stack.push(inFix[i]);
                                     // cout &lt;&lt; "Top Ptr is a: "&lt;&lt; stack.getTopPtr()-&gt;getData() &lt;&lt; endl;
  
  
  
                                 }
  
                      for(int r = 0;r != '\0';r++)
                          cout &lt;&lt; postFix[r] &lt;&lt; " ";
  
                          if(inFix[i] == ')')
                         {
                             while(stack.stackTop()!= '(')
                           {
                                 postFix[j] = stack.pop();
                                 i++;
                                 j++;
                                  }
                             stack.pop();
  
                              }
                         }
             }
  
                     }
  </code></pre>
  
  <p>Note the function convertToPostfix was made using this algorithm:</p>
  
  <ul>
  <li>Push a left parenthesis ‘(‘ onto the stack.</li>
  <li>Append a right parenthesis ‘)’ to the end of infix.</li>
  <li><p>While the stack is not empty, read infix from left to right and do the following:</p>
  
  <ul>
  <li>If the current character in infix is a digit, copy it to the next element of postfix.</li>
  <li>If the current character in infix is a left parenthesis, push it onto the stack.</li>
  <li><p>If the current character in infix is an operator,</p>
  
  <ul>
  <li>Pop operator(s) (if there are any) at the top of the stack while they have equal or higher precedence than the current operator, and insert the popped operators in postfix.</li>
  <li>Push the current character in infix onto the stack.</li>
  </ul></li>
  <li>If the current character in infix is a right parenthesis
  <ul>
  <li>Pop operators from the top of the stack and insert them in postfix until a left parenthesis is at the top of the stack.</li>
  <li>Pop (and discard) the left parenthesis from the stack.</li>
  </ul></li>
  </ul></li>
  </ul>