Last output missing when copying input with multiple lines.

Hello,
When you copy this whole input:
test1
test2
test3

and paste it into the running program below why do you get the output:
test1 test2

instead of:
test1 test2 test3

Moreover, what adjustment in the code can you make to get the latter output?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string line{};
    while(cin >> line)
    {
        cout << line << ' ';
    }
}
Last edited on
Try appending << std::flush; or << std::endl; for explicit flushing.
Last edited on
What is the medium through which you are running this program? I thought blocking on cin was automatically supposed to flush, but I could be mistaken.

Note that it'll only actually send the last line automatically if it ends with a newline, assuming you're using a normal terminal/console.
Last edited on
How are you terminating the while loop? With Windows as an example I get with the input each on a separate line:


test1
test1 test2
test2 test3
test3 ^Z


Or having all the input on one line:


test1 test2 test3
test1 test2 test3 ^Z

Topic archived. No new replies allowed.