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.
May be this question is too broad, I don't know, but if someone can explain with following example please?
int temp;
while (cin >> temp) {
cout << temp << endl;
}
When I input
1 2 3<ENTER>
It prints
1
2
3
as I expected, and press
<CTRL+D>
to quit.
But if I input
1 2 3<CTRL+D>
It prints
1D
2
following with my input character "3", and then I press
<ENTER>
the last character "3" will be printed. Then I should press
<CTRL+D>
again to quit.
And besides, there is a similar example as following:
vector<int> list; int temp;
while (cin >> temp) {
list.push_back(temp);
}
for (auto e : list) {
cout << e << endl;
}
When I input
1 2 3<ENTER><CTRL+D>
It prints
1D
2
3
So my question is:
What happened in these three cases then I press
<CTRL+D>
and
<ENTER>
?
Why there is a "D" character in the second and third case? And how to prevent it from happening?