1

everyone! I recently tried a piece of code from [C++ Primer, 5th edition]. This code is at chapter 3.

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    string word;
    while(cin >> word) {
        cout << word << endl;
    }
    return 0;
}

Then, the book says: "...Once we hit end-of-file (or valid input), we fall out of the while." Surprisingly, I can never jump out from the while statement when trying this code in Visual Studio C++ 2017.

My question is: give what input will jump out from the loop? This book is very famous and read by many people, so I believe there is no error in the code.

Thank you!

zchenkui
  • 139
  • 1
  • 10

1 Answers1

1

In console you can simulate EOF flag. In UNIX systems it is Ctrl+D, in Windows Ctrl+Z. When you type this in the console, program will behave like it has just reached end of input file.