Click to See Complete Forum and Search --> : How to terminate cin?


erxuan
January 27th, 2005, 06:32 PM
I use
vector<string> Names;
copy(istream_iterator<string>(cin), istream_iterator<string>(),back_inserter(Names));
....other code...
to read strings from cin stream.
The problem is I don't know how to terminate the input stream and run the code below it. I tried Enter,"Ctrl+D"...but the screen remain the status of receiving the input...

HighCommander4
January 27th, 2005, 07:48 PM
The default constructor of istream_iterator constructs an iterator that represents past-the-end. The first iterator will only be equal to the past-the-end one (and therefore input will only terminate) if something causes the eofbit or failbit of the stream passed as an argument (in your case, cin), to be set. So one way of terminating input is simulating the end-of-file character from the keyboard, usually done with Ctrl+Z.

I'm not sure if you have any choice other than a default-constructed istream_iterator object as the second argument to copy() in a case like this.