Search code examples
c++stdistream-iterator

istream_iterator copy example keeps waiting for input


I tried implementing an example of stream iterators from page 107 of "The C++ Standard Library". I get stuck on this line:

copy (istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(coll)); 

The program keeps reading data from the console here, but does not pass on to the next line. How do I continue past this point?


Solution

  • take for instance, if you would have made an input stream of int then you would have given input like - 45 56 45345 555 ....., so in all those cases, the reading operation of input stream would have returned a true value - while (cin>>var) { } the while statement will not stop if it is getting a valid input, so to stop the reading of characters, we gave it following input, ... 54 56 3545 | , and as soon as it receives a special character the while loop stops as the conditions returns false.

    That's the same case for all other type of input streams as well.

    So I assume you understand here why your string type input stream never stops taking input because every possible input can be considered string.

    The solution to this problem is using "ctrl + D in UNIX" and "ctrl + Z in windows", as it gives NULL in condition of while loop which means false, hence stoping the reading of string input.