title: C++: Checking if the order of parentheses and brackets from a string is correct using stacks
body: <p>I have to create a function using pop() and push() to check if the order of the parentheses and brackets is correct, for exemple &quot;( ( [ [ ] ] ) )&quot; is correct, but &quot;( ( ( [ ] ] ) )&quot; is not.</p>
  <p>My idea was to make a loop and for each iteration get a char from the string and check if its an opening (bracket or parentheses) then push it, if it's a closing then check if the top() from stack is an opening of the same kind, if it is not then return false, if it is then pop() and continue the loop, until the string ends.</p>
  <p>The problem is that I don't know why my code doesn't work.</p>
  <p>Thanks in advance!</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stack&gt;  
  using namespace std;
  
  bool checkExpress(string str){
      int i;
      stack &lt;char&gt; st;
      
      if(str.length() % 2 != 0) return false;
      
      for(i=0; i &lt; str.length(); i++){
  
          if(str[i] == '(' || str[i] == '[') st.push(str[i]);
          
          else if(str[i] == ')' || str[i] == ']'){
  
              if(str[i] == ']' &amp;&amp; st.top() != '[') return false;
  
              else if(str[i] == ')' &amp;&amp; st.top() != '(') return false;
          }
  
          st.pop();   
      }
  
      return true;
  }
  
  
  int main(){
      string a = &quot;(([[]]))&quot;;
      
      if(checkExpress(a)) cout &lt;&lt; &quot;The expression is well constructed.&quot;;
      else cout &lt;&lt; &quot;The expression is not well constructed.&quot;;
  
      return 0;
  }
  </code></pre>