I have come to a question in C++ Primer (p 314 , exercise 8.1) that is not clear to me.
"Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream."
Breaking this down, the function has to do three things:
1. Read a stream until it hits end-of-file
So the >> operator reads input from an istream object - cin.
This stream's end of file can be interrogated by
cin.eof()
. This returns true if the end of file bit is set which can be tested with a bool variable
1 2 3 4 5 6
|
bool on = false;
on = cin.eof();
if(on == true)
// end of file is reached, else
if(on ==false)
// keep reading cin
|
I don't believe that this is completely correct so can someone show me how this code should be presented?
2. Print what is read to the standard output
I can only imagine this to be cout << ? But am lost from here
3. Reset the stream so it is valid before returning the stream
This section of the problem again defeats me.
Can anyone help with this function?