As I am going through Programming Principels and Practices using C++, it says for a string, >>reads whitespace-separated words. ....terminate the loop by giving the program and end-of-file character(Ctrl+z, shown as '^z').
if I input "abc def ^z" followed by enter, the program goes on, and ask for more input instead of ending the loop. I go on input more word "abc" enter. it prints ?abc. If I input "^z" and enter, supposedly when there is not character before the end of file, it terminates the loop properly.
Can anyone help me understand this behavior?
1 2 3 4 5 6 7
while(cin>>current)
{
cout << "current word: " << current << "\n";
if (current == previous)
cout << "repeated word: " << current << "\n";
previous = current;
}
First what operating system are you using? Ctrl-Z is only the end of file character for Linux/Unix Operating systems. For Windows the end of file character is Ctrl-D.
Second usually the end of file may need to be the only thing on the line.
@jlb, you've got that backwrds. Nix is Ctrl-D, Windows is Ctrl-Z.
The Ctrl-Z needs to be the first "character" on a line.
In a sense it's not really a character at all, but just a signal to the command shell to send the actual eof signal to the program.
Apparently it's set up to only send the eof signal if the Ctrl-Z is the first (and only?) thing on the line.
(Ctrl-Z as an actual character used to be stored at the end of windows text files, IIRC. It was possible to put extra text after a Ctrl-Z that wouldn't show up in a text editor that respected the Ctrl-Z file ending.)