the_drow
the_drow

Reputation: 19201

Copying from istream never stops

This bit of code runs infinitely:

copy(istream_iterator<char>(cin), istream_iterator<char>(), back_inserter(buff));

The behavior I was expecting is that it will stop when I press enter.
However it doesn't.
buff is a vector of chars.

Upvotes: 0

Views: 237

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 186068

I assume you are typing stuff in at the keyboard.

The enter key doesn't signify the end of the stream. It's just another character from cin's perspective. You need to submit EOF to achieve this (Ctrl+Z, Enter on Windows and Ctrl+D on Unix/Mac).

Incidentally, this isn't the usual way to read characters from the console. It is very inefficient (istream_iterator calls operator>> for each character) and will misbehave with whitespace. To read a line of data entry, use getline instead.

Upvotes: 2

Related Questions