title: C++ std::stack empties after each iteration of while loop
body: <p>I'm trying to write a function used to parse XML tags for XML like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Employee&gt;
      &lt;salary&gt;40000&lt;/salary&gt; &lt;name&gt;John Doe&lt;/name&gt; &lt;id&gt;1234&lt;/id&gt;
      &lt;address&gt;2230 W. Treeline Dr.&lt;/address&gt;
      &lt;city&gt;Tucson&lt;/city&gt;
      &lt;state&gt;Arizona&lt;/state&gt;
      &lt;country&gt;USA&lt;/country&gt;
      &lt;phone&gt;520-742-2448&lt;/phone&gt;
  &lt;/Employee&gt; &lt;Employee&gt;
      &lt;salary&gt;60000&lt;/salary&gt; &lt;name&gt;Jane Doe&lt;/name&gt; &lt;id&gt;4321&lt;/id&gt; &lt;/Employee&gt;
  &lt;Employee&gt;
      &lt;salary&gt;140000&lt;/salary&gt;
      &lt;state&gt;Michigan&lt;/state&gt;
      &lt;name&gt;Jack Dough&lt;/name&gt;
      &lt;id&gt;12345&lt;/id&gt;
      &lt;city&gt;Dearborn&lt;/city&gt;
      &lt;country&gt;USA&lt;/country&gt;
      &lt;phone&gt;303-427-0153&lt;/phone&gt;
      &lt;address&gt;24437 Princeton&lt;/address&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>I'm using an <code>std::stack</code> to keep track of the tags. When an opening tag (e.g. <code>&lt;Employee&gt;</code>) is encountered, a string representation of the tag name (<code>Employee</code>) is pushed onto the stack, and, assuming the XML is written correctly, encountering the matching closing tag (e.g. <code>&lt;/Employee&gt;</code>) causes the tag name to be popped off of the stack. However, I have run into an issue where a string is pushed onto the stack so the stack has a size of 1. When the body of the while loop is reached and the body is executed a second time, the stack is emptied. I can't for the life of me figure out what I'm doing wrong. Why does my stack "reset" every time the <code>while</code> loop reiterates?</p>
  
  <pre class="lang-cpp prettyprint-override"><code>static void fromXML(std::istream&amp; in_stream) {
      std::stack&lt;std::string&gt; tags;
      std::string temp;
      std::string tagName;
      std::getline(in_stream, temp);
      std::istringstream ss(temp);
  
      bool building_employee = true;
      int _id;
      std::string _name;
      std::string _address = "";
      std::string _city = "";
      std::string _state = "";
      std::string _country = "";
      std::string _phone = "";
      double _salary = 0.0;        
  
      while(building_employee &amp;&amp; std::getline(ss, temp, '&lt;')) {
          std::cout &lt;&lt; "Size: " &lt;&lt; tags.size() &lt;&lt; std::endl; // Prints "0"
  
  
          std::getline(ss, tagName, '&gt;');
          ss.ignore(256, '&lt;');
  
          if (tagName == "") { }
          else if ( !tags.empty() &amp;&amp; tagName == "/" + tags.top() ) {
              std::cout &lt;&lt; "Popping " &lt;&lt; tags.top() &lt;&lt; std::endl;
              tags.pop();
          }
          else if ((tags.empty() &amp;&amp; tagName == "Employee") || (!tags.empty() &amp;&amp; tagName != "Employee")) {
              std::cout &lt;&lt; "Pushing " &lt;&lt; tagName &lt;&lt; std::endl;
              tags.push(tagName);                
          }
          else {                 
              std::cerr &lt;&lt; "Problem with XML." &lt;&lt; std::endl;
          }
  
          if (tagName == "/Employee") { building_employee = false; }
          else if (tagName == "id") { _id = std::stoi(temp); }
          else if (tagName == "name") { _name = temp; }
  
  
          if (!building_employee) { std::cout &lt;&lt; "Employee end." &lt;&lt; std::endl; }
          if(in_stream.eof()) {
              std::cout &lt;&lt; "Error: End of file reached without closing &lt;" &lt;&lt; temp &lt;&lt; "&gt;" &lt;&lt; std::endl;
          }
          std::cout &lt;&lt; "Size: " &lt;&lt; tags.size() &lt;&lt; std::endl; // Prints "1" after pushing one string onto the stack
      }
      if (temp == "/Employee") { std::cout &lt;&lt; "Employee object succesfully created" &lt;&lt; std::endl; }
  }
  </code></pre>