title: Iterator not dereferencable stack (expression tree) & loop does not work properly
body: <p>I am trying to convert infix to postfix and prefix by using tree in c++. I pass the infix expression to the createTree function and in the code I use 2 stack node(to store the operand and operator that has been poped from the stack s) and stack s(to store the operator). The code works finely when I input expression that has same precedence operators or the first operator has higher precedence than the second one. ex: 1*2/3 or 1/2+3. But it will debug when I input expressions which the first operator has lower precedence than the second one. for example : 1+2/3.
  I try to debug it and found DEBUG_ERROR("iterator not dereferencable"); I try to find in the internet and all those problems related to vector which I have never used before. 
  I hope you guys could help me</p>
  
  <pre><code>  node *CreateTree(string post)
  {
  node *temp, *op1 , *op2;
  stack&lt;node*&gt; tree;
  stack&lt;char&gt; s;
  int i = 0;
  while (!tree.empty())
  {
      tree.pop();
  }
  for(i = 0; i&lt;post.length();i++)
  {
      if (post[i]==' ') continue;
      else if(IsOperand(post[i]))
      {
          node *temp = new node();
          temp-&gt;in = post[i];
          temp-&gt;left = NULL;
          temp-&gt;right = NULL;
  
          for(int j=i+1;j&lt;post.length();j++)
          {
              if (IsOperand(post[j]))
              {
                  temp-&gt;in+=post[j];
                  i = j;
              }
              else break;
          }
          tree.push(temp);
      }
      else if(isOperator(post[i]))
      {
          while(!s.empty() &amp;&amp; s.top()!='(' &amp;&amp; s.top()!='{' &amp;&amp; s.top()!='[' &amp;&amp; !hasHigherPrecedence(post[i],s.top()))
         {
              node *op1;
              node *op2;
              op2 = tree.top();
              tree.pop();
              op1 = tree.top();
              tree.pop();
              node *temp = new node();
              temp-&gt;in = s.top();
              temp-&gt;left = op1;
              temp-&gt;right = op2;
              root = temp;
              s.pop();
              tree.push(root);
         }
          s.push(post[i]);
      }
      else if(post[i]=='(')
     {
         s.push(post[i]);
     }
     else if(post[i]==')')
     {
         while(s.top()!='('&amp;&amp;!s.empty())
         {
              node *op1;
              node *op2;
              op2 = tree.top();
              tree.pop();
              op1 = tree.top();
              tree.pop();
              node *temp = new node();
              temp-&gt;in = s.top();
              temp-&gt;left = op1;
              temp-&gt;right = op2;
              root = temp;
              s.pop();
              tree.push(root);
         }
         s.pop();
     }
     else if(post[i]=='[')
     {
         s.push(post[i]);
     }
     else if(post[i]==']')
     {
         while(s.top()!='['&amp;&amp;!s.empty())
         {
              node *op1;
              node *op2;
              op2 = tree.top();
              tree.pop();
              op1 = tree.top();
              tree.pop();
              node *temp = new node();
              temp-&gt;in = s.top();
              temp-&gt;left = op1;
              temp-&gt;right = op2;
              root = temp;
              s.pop();
              tree.push(root);
         }
         s.pop();
     }
     else if(post[i]=='{')
     {
         s.push(post[i]);
     }
     else if(post[i]=='}')
     {
         while(s.top()!='{'&amp;&amp;!s.empty())
         {
              node *op1;
              node *op2;
              op2 = tree.top();
              tree.pop();
              op1 = tree.top();
              tree.pop();
              node *temp = new node();
              temp-&gt;in = s.top();
              temp-&gt;left = op1;
              temp-&gt;right = op2;
              root = temp;
              s.pop();
              tree.push(root);
         }
         s.pop();
     }
     else cout&lt;&lt;post[i]&lt;&lt;" is not registered as operator or operand";
  }
  while (!s.empty())
  {
      node *op1;
      node *op2;
      op2 = tree.top();
      tree.pop();
      op1 = tree.top();
      tree.pop();
      node *temp = new node();
      temp-&gt;in = s.top();
      temp-&gt;left = op1;
      temp-&gt;right = op2;
      root = temp;
      s.pop();
  }
  return root;
  }
  </code></pre>
  
  <p>below is the functions that are called in code above</p>
  
  <pre><code>bool isOperator(char a)
  {
        return(a=='+' || a=='*' || a=='^' || a=='/' ||
         a=='-' || a=='%')? true:false;
  }
  
  int getWeight(char op)
  {
   int weight;
   switch(op)
  {
      case '+' : case '-':
          weight = 1;
          break;
      case '*':
      case '/':
      case '%':
          weight = 2;
          break;
      case '^':
          weight = 3;
          break;
  
  }
  return weight;
  
  }
  
  bool hasHigherPrecedence(char a, char top)
  {
     int aW = getWeight(a);
     int tW = getWeight(top);
     return (aW &gt; tW)? true : false;
  }
  </code></pre>