title: C++ - Queue pop not working corrrectly using using for() loop
body: <p>While writing a simple code using STL queue and <code>for</code> loop, I faced a problem. My procedure is simple: take a number as a string, convert them into queue elements and show them.</p>
  
  <p>My code is below : </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;queue&gt;
  #include&lt;string&gt;
  //#include&lt;cctype&gt;
  using namespace std;
  
  //int to_words(int i);
  //int to_words_one(queue&lt;int&gt; &amp;q);
  
  int main()
  {
  queue&lt;int&gt; q;
  string s;
  cout &lt;&lt; "Enter a number not more than 12 digits : ";
  getline(cin, s, '\n');
  for (int i = 0; i &lt; s.length(); i++)
  {
      if (!isdigit(s[i]))
      {
          cout &lt;&lt; "Not a valid number." &lt;&lt; endl;
          s.clear();          
          break;
      }
  }
  if(s.size() &gt; 0)
     for (int i = 0; i &lt; s.length(); i++)
        q.push(s[i] - '0');
  
  while (!q.empty())
  {
      cout &lt;&lt; q.front();
      q.pop();
  }
  system("PAUSE");
  }
  </code></pre>
  
  <p>It works fine. No problem. But instead of <code>while(!q.empty())</code>, if I use </p>
  
  <pre><code>for(int j=0;j &lt; q.size(); j++)
  {
     cout &lt;&lt; q.front();
     q.pop();
  }
  </code></pre>
  
  <p>It doesn't work properly! It just shows and pops some first elements , and NOT ALL elements of the queue and prompts for <code>Press any key to continue</code>. Please tell me why is this happening? Shouldn't <code>while(!q.empty())</code> and and that <code>for()</code> loop work similar?</p>