title: Strange cout behavior in a while loop in C++
body: <p>I am trying to print the input and then all the strings in the tk vector, as in the following program:</p>
  
  <pre><code>int main() {
      while (true) {
          string input;
          cout &lt;&lt; "prompt: ";
          cin &gt;&gt; input;
          vector&lt;string&gt; tk = {"this", "is", "an", "example"}; 
  
          cout &lt;&lt; input &lt;&lt; endl;
          for (int i = 0; i &lt; tk.size(); i++) {
              cout &lt;&lt; tk[i] &lt;&lt; endl;
          }
  
      }   
      return 0; 
  }
  </code></pre>
  
  <p>When I give the input "Hello world" I am expecting the output to be:</p>
  
  <pre><code>Hello world
  this
  is
  an 
  example
  prompt: 
  </code></pre>
  
  <p>But the output was:</p>
  
  <pre><code>Hello
  this
  is
  an
  example
  prompt: world
  this
  is 
  an
  example
  prompt:
  </code></pre>
  
  <p>Does anyone know what went wrong here? I guess the cause is related to how the buffer works, but I really have no idea about the details.</p>