aredscout
aredscout

Reputation: 33

Do stuff with cin buffer until empty

I want to have my program read through the cin buffer until there's nothing left in it to read.

What a lot of places seemed to suggest for this was:

while (cin)
{
    cin >> s;       
    //do stuff with s
}

However, when I try this, even while the cin buffer is empty, the program just infinitely waits for input and executing the loop.

Doing while(cin >> s) does the exact same thing.

Upvotes: 0

Views: 4707

Answers (2)

antlersoft
antlersoft

Reputation: 14786

If standard input is attached to a terminal, the program will wait for input until it gets an EOF condition, which you can send on POSIX systems with a Ctrl-D

Upvotes: 2

Ryan Johnson
Ryan Johnson

Reputation: 23

You are correct it will wait forever for a new line.

What you could do is this:

while(cin >> s){
    //do stuff with s
}

What this does is while you can still read into s then do stuff. You will eventually hit the end of the buffer and it will stop.

I believe with the way you are doing it, the computer is thinking that it could still have some information come in.

Upvotes: 1

Related Questions