Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened when press ctrl+D in while(cin>>i) loop in C++?

Tags:

c++

iostream

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?

like image 836
Qian Wang Avatar asked Nov 09 '22 10:11

Qian Wang


1 Answers

It is probably due to overlapping between the console output and yours.

When you press ctrl+D, the console prints ^D but print a return character ('\r') immediately afterward. This means that everything that will be printed after that, will be printed over the ^D.

Your program first prints 1, so it only overlaps the ^, leaving the D intact. Try inserting a two digit number and you will see that the D disappears.

Some consoles wipe the line after the return character, but some don't as probably your console does.

Solutions

(1) You can simply print '\n' (std::endl) before you start printing the list. This will leave the ugly ^D in the console but it will not overlap your print.

vector<int> list; int temp;
while (cin >> temp) {
    list.push_back(temp);
}
cout << endl; // Print before the list.
for (auto e : list) {
    cout << e << endl;
}

(2) You can print an empty line first to delete this print. Just print: " \r" (i.e., two spaces then a return character).

int temp;
while (cin >> temp) {
    cout << "\r  \r" << temp << endl; // Print in each line
}
like image 123
Liran Funaro Avatar answered Nov 14 '22 21:11

Liran Funaro