E
E
Eugene2014-10-10 22:01:31
C++ / C#
Eugene, 2014-10-10 22:01:31

Cin how to detect the end of a stream?

There is such a program:

string n;
while (cin >> n) {
    cout << n;
}

It is necessary to read the stream (everything except for whitespace characters) and display it on the screen. How to organize an exit from the loop if there are only whitespace characters in the stream?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Misha Krinkin, 2014-10-11
@Jek_Rock

if there are only whitespace characters in the stream, cin >> n will simply ignore them, and if it reaches the end of the stream, then cin >> n will be interpreted as false, in fact, the exit will happen anyway. I don't think you've covered the whole issue...

S
Stanislav Silin, 2014-10-10
@byme

You can catch the end of a line:

string n;
do
{ 
     cin >> n; 
    cout << n;
}while(cin.peek() != '\n'))

But not the end of the console stream. With a file stream, things are different, there is an eof () method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question