What happened when press ctrl+D in while(cin>>i) loop in C++?
<p>I am new to C++ and feel a little confused with this question. I use Mac OS X and command "g++ -std=c++11" to compile the code.</p>

<p>May be this question is too broad, I don't know, but if someone can explain with following example please?</p>

<pre><code>int temp;
while (cin &gt;&gt; temp) {
    cout &lt;&lt; temp &lt;&lt; endl;
}
</code></pre>

<p>When I input</p>

<pre><code>1 2 3&lt;ENTER&gt;
</code></pre>

<p>It prints </p>

<pre><code>1
2
3
</code></pre>

<p>as I expected, and press</p>

<pre><code>&lt;CTRL+D&gt;
</code></pre>

<p>to quit.</p>

<p>But if I input</p>

<pre><code>1 2 3&lt;CTRL+D&gt;
</code></pre>

<p>It prints </p>

<pre><code>1D
2
</code></pre>

<p>following with my input character "3", and then I press </p>

<pre><code>&lt;ENTER&gt;
</code></pre>

<p>the last character "3" will be printed. Then I should press </p>

<pre><code>&lt;CTRL+D&gt; 
</code></pre>

<p>again to quit.</p>

<p>And besides, there is a similar example as following:</p>

<pre><code>vector&lt;int&gt; list; int temp;
while (cin &gt;&gt; temp) {
    list.push_back(temp);
}
for (auto e : list) {
    cout &lt;&lt; e &lt;&lt; endl;
}
</code></pre>

<p>When I input</p>

<pre><code>1 2 3&lt;ENTER&gt;&lt;CTRL+D&gt;
</code></pre>

<p>It prints</p>

<pre><code>1D
2
3
</code></pre>

<p>So my question is:</p>

<p>What happened in these three cases then I press </p>

<pre><code>&lt;CTRL+D&gt; 
</code></pre>

<p>and </p>

<pre><code>&lt;ENTER&gt;
</code></pre>

<p>?</p>

<p>Why there is a "D" character in the second and third case? And how to prevent it from happening? </p>