This is the exact excerpt from the book. Doesn't mention anything about end of file code or terminating the loop.
The code sequencestring word;while ( cin >> word ) // ...reads one string from standard input with each iteration of the while loop until all the strings are read. The condition( cin >> word )evaluates to false when the end-of-file is reached (how this occurs is explained in Chapter 20). Here is a simple program that uses the code sequence:#include <iostream>#include <string>int main(){ string word; while ( cin >> word ) cout << "word read is: " << word << '\n'; cout << "ok: no more words to read: bye!\n"; return 0;}The following are the first five words of James Joyce's novel Finnegans Wake:riverrun, past Eve and Adam'sWhen these words are entered at the keyboard, the output of the program is as follows:word read is: riverrun,word read is: pastword read is: Eveword read is: andword read is: Adam'sword read is: ok: no more words to read: bye!
Quote:Original post by Hodgman
Yeah the code is correct. But it's assuming that you're going to type in an EOF character (this used to be common knowledge when UNIX was king).
Try ctrl+C or ctrl+D or ctrl+Z or something...
Ctrl+C ends the program and cuts off the command line so it says,
wor^C
Ctrl+Z causes an endless loop that needs to be ended with ctrl+C and ctrl+D is a special character.
Ctrl+C seems to be what it needs, it definitely ends the loop but it can't output the full line.
I'm using Windows, not Unix. The book didn't specificly say, use UNIX and is usually worded with instructions for both. There wasn't a special instruction with this though.