title: C++ Program for Checking Correct HTML Tag Using Stack
body: <p>I have been trying to follow an example code on my data structure textbook.
  It is a program to check  balance of parenthesis in C++ using a stack but it is not working as expected. Somehow I am having a problem with HTML tag matching algorithm.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stack&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  vector&lt;string&gt; getHtmlTags() {                                      //receives a string of html tags and divide them by "&lt;, &gt;"
      vector&lt;string&gt; tags;                                            //vector of html tags
      while (cin) {                                                   //reads the whole input
          string line;
          getline (cin,line);
          int pos = 0;                                                //current scanning position
          int ts = line.find("&lt;",pos);                                //scans from the current scanning position
          while (ts!=string::npos) {                                  //repeat until end of string
              int te = line.find("&gt;", ts+1);                          //scans for the end of a tag (&lt;)
              tags.push_back(line.substr(ts,te-ts+1));                //save tag to the vector
              pos = te + 1;                                           //repositioning
              ts = line.find("&lt;",pos);
          }
      }
      return tags;                                                    //return vector of tags
  }
  
  
  bool isHtmlMatched(const vector&lt;string&gt;&amp; tags) {                    //checks if the html tags are correctly matched
      stack&lt;string&gt; S;                                            //implememted stack from above for opening tags
      typedef vector&lt;string&gt;::const_iterator Iter;                    //iterate through vector
  
      for (Iter p = tags.begin(); p != tags.end(); ++p) {             
          if (p-&gt;at(1) != '/')                                        //is it the opening tag?
              S.push(*p);                                             //push to the stack
          else{
              if (S.empty()) return false;                            //there is nothing to match
              string open = S.top().substr(1);                        //opening tag excluding '&lt;'
              string close = p-&gt;substr(2);                            //closing tag excluding '&gt;'
              if (open.compare(close) != 0) return false;             //exception for fail to match
              else S.pop();                                           //pop matched element
  
          }
      }
      if (S.empty()) return true;                                     //everything has matched correctly - Correct
      else return false;                                              //some did not match correctly - Incorrect
  }
  
  int main() {
  
      int rep;                                                        //decides the number of trial
      cin &gt;&gt; rep;
  
      for (int i=1; i&lt;=rep; i++) {                                    //loop up to the decided trial
          if(isHtmlMatched(getHtmlTags()))
              cout &lt;&lt; "Correct" &lt;&lt; endl;
          else cout &lt;&lt; "Incorrect" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>It runs without a compilation error, but for some reason the console does not respond after I throw a list of HTML tags.
  I would like to ask for help for improving these codes. Any ideas are appreciated. </p>