[SOLVED]GNU C++ Question

I was reading the C++ Primer book (just started) written by Stanley B. Lippman and Josee Lajoie (who at the time of print was chair of the core language group of ANSI/ISO C++ Standard Committee). I did an example in the first chapter about input and output (cin/cout).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string word;
   
   while (cin >> word)
      cout << "word read is: " << word << '\n';
     
   cout << "ok: no more words to read: bye!\n";
   
   return 0;
}


With the input of "riverrun, past Eve and Adam's" which should have did output of each word followed by "ok: ....". Instead it just sits waiting for more input while ideone worked like I expected ( http://ideone.com/1KdlB ).

Now my question: Does this mean I need a more strict compiler or do I need to pass flags during compilation to make it more strict to run like I expected? Guess it also raises the question: "Does it mean the standard has changed enough to where that example is wrong now making ideone compiler outdated?"
Last edited on by closed account z6A9GNh0
Nothing is wrong. Reading from the console, cin cannot know when you want to stop. Ideone, however, does not read from the console, it reads from a file.
Okay, so that means the book is wrong. It mentioned EOF before the code, but right after that code it says:

The following are the first five words of James Joyce's novel Finnegan Wake: riverrun, past Eve and Adam's
When these words are entered at the keyboard, the output of the program is as follows:
word read is: riverrun,
word read is: past
word read is: Eve
word read is: and
word read is: Adam's
word read is: ok: no more words to read: bye!

When I looked at the example I thought it was wrong as I remember reading that the input stream never ends rather it goes into a fail state, but thought I would ask in case I was thinking wrong and not getting the desired effects I was expecting because of the book showing the example output.
I remember reading that the input stream never ends rather it goes into a fail state

Trying to read from an ended stream will fail. ¿So what's the problem?

Reading from the console, cin cannot know when you want to stop
You could use EOF. Like cat <C-D> or copy con <C-Z> do.
Well EOF only applies to files (doesn't it?) and the code they showed to demonstrate got the input form the keyboard so there was no EOF to check for. The input stream only goes into failed state if you are expecting one thing and get something else instead so you have to flush the input stream at that point. With the code they have in the example kind of guarantees no fail state by the looks of it because a string being used.
No, that makes the behavior stay the same for the program code they had without having to press Enter.
In Unix the end-of-file character causes the terminal driver to make available all characters its input buffer immediately.
If the input buffer is empty, a program reading from the terminal reads a count of zero bytes. In Unix, such a condition is understood as having reached the end of the file.
Don't understand your complain.
That must be wrong for Ubuntu then. Even with that the program never leaves the while loop to print the exit string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string word;
   
   while (cin >> word)
      cout << "word read is: " << word << '\n';
     
   cout << "ok: no more words to read: bye!\n";
   
   return 0;
}

It never exits the loop like the book example had claimed. Since the input stream never is empty (unless it goes into fail state which is probably impossible with the string type) and never has an EOF command it just prints out the sentences/words you type for each word in it then sits waiting for more words rather than ending with the last word and outputting "ok: ....". The method you told me to try just made the program act like it did and just constantly waits for input from the keyboard. This just makes me understand that the example is flawed a little.
The input buffer will be emptied if you press Enter. Or ctrl+d
Once empty, pressing ctrl+d will trigger EOF in the stream, sending it to a fail state.

If that is not happening, issue a bug to your compiler and OS vendor.
Yeah I found out where the issue was. The article you posted said to do CTRL+D before ENTER which was just flushing it but not sending it to fail state but instead resetting it so it was waiting for more input. You have to hit ENTER the CTRL+D for it to make the app run like it was meant to run. Tested this multiple times to double check, apparently if you do CTRL+D it just acts like ENTER in Ubuntu.
Topic archived. No new replies allowed.