No output with cout

What's causing this?


The first instruction in main:
cout << "We are alive"; // this is not printed, rest of program works fine


But when I add "endl" it is printed
cout << "We are alive" << endl;
Buffering, probably. Output is inherently slow, so endl flushes the output before moving on; without it, the output may buffer until it is convenient.

How about showing compileable code that does what you claim?

Here's one possibility. Without flushing, the output may never get shown as the program exits before buffered output gets written.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
   cout << "We are alive"; // Then try << endl;

   try{ cout << 4/0; }
   catch(...){}
}
Last edited on
division by 0 does not throw a std::exception (you cannot catch it)
You can on Windows if SEHs are mapped to C++ exceptions, as they used to be.

But yeah, you're right as this is a Unix chat.
Last edited on
Topic archived. No new replies allowed.