Sep 12, 2022 at 12:35pm
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 Sep 12, 2022 at 12:36pm
Sep 12, 2022 at 12:39pm
Try appending << std::flush;
or << std::endl;
for explicit flushing.
Last edited on Sep 12, 2022 at 12:42pm
Sep 12, 2022 at 12:42pm
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 Sep 12, 2022 at 12:43pm