title: c++ Delimiter Matching
body: <p>This program basically just checks that a statement (mathematical) is properly constructed considering the brackets, parenthesis, and curly-braces. It uses a stack to compare the most recently read  delimiter to see if the ending type of that delimiter exists, otherwise it says it is not constructed properly. I'm stuck, as it always returns that the statement is incorrect.  This is what I have so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stack&gt;
  using namespace std;
  
  bool delimiterMatching(char* file);
  
  void main(){
      char fileName[50];
  
      cout &lt;&lt; "Enter a statement (Ex. s=t[5]+u/(v*(w+y));   : ";
      cin &gt;&gt; fileName;
      cout &lt;&lt; endl;
  
      if(delimiterMatching(fileName))
          cout &lt;&lt; endl &lt;&lt; "Your statement was constructed successfully." &lt;&lt; endl;
      else cout &lt;&lt; endl &lt;&lt; "Your statement is incorrectly constructed." &lt;&lt; endl;
  }
  
  bool delimiterMatching(char* file){
      stack&lt;char&gt; var;
      int counter = 0;
      char ch, temp, popd;
      do{
          ch = file[counter];
          if(ch == '(' || ch == '[' || ch == '{')
              var.push(ch);
          else if(ch == '/'){
              temp = file[counter+1];
              if(temp == '*')
                  var.push(ch);
              else{
                  ch = temp;
                  continue;
              }
          }
          else if(ch == ')' || ch == ']' || ch == '}'){
              popd = var.top();
              var.pop();
              if(ch != popd)
                  return false;
              }
          else if(ch == '*'){
              temp = file[counter+1];
              popd = var.top();
              var.pop();
              if(temp == '/' &amp;&amp; popd != '/')
                  return false;
              else{
                  ch = temp;
                  var.push(popd);
                  continue;
              }
          }
          counter++;
      }while(ch != '\n');
      if(var.empty())
          return true;
      else return false;
  }
  </code></pre>
  
  <p>I've already tried googling for some hints, but got nothing too helpful. I debugged it, and if I use "s=t[5]+u/(v*(w+y));", when it reads the second bracket after the 5, it obviously isn't the same char. So how do I compare the opening symbol with the ending symbol?</p>
  
  <p>I appreciate any help. I'll edit this/comment it if I figure it out myself. Thanks for your time!</p>
  
  <p>I got it working this is the final code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stack&gt;
  using namespace std;
  
  bool delimiterMatching(char* file);
  
  void main(){
      char fileName[50];
  
      cout &lt;&lt; "Enter a statement (Ex. s=t[5]+u/(v*(w+y));   : ";
      cin &gt;&gt; fileName;
      cout &lt;&lt; endl;
  
      if(delimiterMatching(fileName))
          cout &lt;&lt; endl &lt;&lt; "Your statement was constructed successfully." &lt;&lt; endl;
      else cout &lt;&lt; endl &lt;&lt; "Your statement is incorrectly constructed." &lt;&lt; endl;
  }
  
  bool delimiterMatching(char* file){
      stack&lt;char&gt; var;
      int counter = 0;
      char ch, temp, popd;
      do{
          ch = file[counter];
          if(ch == ';')
              break;
          if(ch == '(' || ch == '[' || ch == '{')
              var.push(ch);
          else if(ch == '/'){
              temp = file[counter+1];
              if(temp == '*')
                  var.push(ch);
              else{
                  counter++;
                  continue;
              }
          }
          else if(ch == ')' || ch == ']' || ch == '}'){
              popd = var.top();
              var.pop();
              if((ch==')' &amp;&amp; popd!='(') || (ch==']' &amp;&amp; popd!='[') || (ch=='}' &amp;&amp; popd!='{'))
                  return false;
          }
          else if(ch == '*'){
              temp = file[counter+1];
              popd = var.top();
              var.pop();
              if(temp == '/' &amp;&amp; popd != '/')
                  return false;
              else{
                  counter++;
                  var.push(popd);
                  continue;
              }
          }
          counter++;
      }while(ch != '\n');
      if(var.empty())
          return true;
      else return false;
  }
  </code></pre>