Here is the simplest program:

#include <iostream>
using namespace std;

int main()
{
    cout << "hello" << endl;
    cin.get();
   return 0;
}

When I ran it (using Dev C++), there is no output, just a blank screen. The problem seems to be from the "endl". If I delete it, or to replace it with
"hello\n"
then it displays the output "hello" as expected.
It would also work well if I just add another cout statement after the original cout, while keeping the "endl".
Why is the problem with "endl"?
Thanks in advance!

Recommended Answers

All 15 Replies

Here is the simplest program:

#include <iostream>
using namespace std;

int main()
{
    cout << "hello" << endl;
    cin.get();
   return 0;
}

When I ran it (using Dev C++), there is no output, just a blank screen. The problem seems to be from the "endl". If I delete it, or to replace it with
"hello\n"
then it displays the output "hello" as expected.
It would also work well if I just add another cout statement after the original cout, while keeping the "endl".
Why is the problem with "endl"?
Thanks in advance!

That is strange behavior. I'm curious if the output isn't getting flushed like ti should. Try this: cout << "hello" << endl << flush; The endl keyword is supposed to flush the output buffer, but perhaps your compiler doesn't add that functionality.

That ran fine for me, you may be having a problem because the cin.get is in there, in order for me to finish executing the program I had to press return again and then it terminated.

cin.get() isn't doing anything, its just sitting in your code trying to look pretty and getting in the way :)

That is strange behavior. I'm curious if the output isn't getting flushed like ti should. Try this: cout << "hello" << endl << flush; The endl keyword is supposed to flush the output buffer, but perhaps your compiler doesn't add that functionality.

Yes, after adding "flush", the output came out! Thanks! Been doing C++ for a long while but never noticed this problem. Why didn't it happen anywhere else, since I have written many programs which ended with "endl" and never had problem?

P.S. logicmonster: cin.get() can't be the problem, since change about that makes no difference. Without that, any output would flash and go away.

cin.get() isn't doing anything, its just sitting in your code trying to look pretty and getting in the way :)

Not if you run the program in an IDE.

Yes, after adding "flush", the output came out! Thanks! Been doing C++ for a long while but never noticed this problem. Why didn't it happen anywhere else, since I have written many programs which ended with "endl" and never had problem?

P.S. logicmonster: cin.get() can't be the problem, since change about that makes no difference. Without that, any output would flash and go away.

Change compilers lately?

Not if you run the program in an IDE.

I didn't know that, now I do. Thanks!

Yes, after adding "flush", the output came out! Thanks! Been doing C++ for a long while but never noticed this problem. Why didn't it happen anywhere else, since I have written many programs which ended with "endl" and never had problem?

P.S. logicmonster: cin.get() can't be the problem, since change about that makes no difference. Without that, any output would flash and go away.

Ok, so the problem was that endl wasn't flushing the output buffer like it is supposed to. See, when you use cout, the output doesn't get sent to the output device immediately. Instead, it is held in a buffer until the operating system decides to send the output to the device. So, the output in your code was just waiting in the output buffer while the OS asked for input. The endl keyword is supposed to trigger a flush, but obviously didn't. A flush simply tells the OS to send all output waiting in the output buffer to the output device now. In c++ you can use the flush keyword to do this explicitly. Also, having a newline character ('\n') in a string sent to output is supposed to flush output as is the endl keyword.

It really is strange that your compiler didn't include a flush with endl, and I would seriously consider using a different one

Change compilers lately?

No, I have always used the same compiler. :icon_rolleyes:

No, I have always used the same compiler. :icon_rolleyes:

Which is?

Which is?

Bloodshed Dev C++.

Bloodshed Dev C++.

Bloodshed Dev C++ is actually the IDE, i.e not a compiler. Perhaps you are using MingW's GCC?

Bloodshed Dev C++ is actually the IDE, i.e not a compiler. Perhaps you are using MingW's GCC?

Yes, according to its website, "It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. "

Please delete, i dont' know what I was thinking :P

that must be an old version cause my mingw runs it just fine try updating the compiler or you can try codeblocks to see if you like it.

disreguard that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.