7

I have a problem with Eclipse IDE for C/C++ Developers.

I'm writting a smal tool for converting Strings. While testing on some point eclipse stopped to give console output. e.g.:
cout<<"test";
doesn't get displayed.

But it's not every where... another example:

// File path as argument
int main(int argc, char* argv[]) {
if (argc != 2) {
    cout
            << "ERROR: Wrong amount of arguments! Only one allowed...\n";
    cout << "\n" << "Programm closed...\n\n";
    exit(1);
}

CommandConverter a(argv[1]);
cout<<"test";
a.getCommandsFromCSV();
cout<<"test2";

return 0;
}

The error message is displayed correctly if the argument is missing. But if the argument is there and the program continues the test outputs:

cout<<"test";
cout<<"test2";

are not displayed...
I am missing something obvious?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Beasly
  • 1,517
  • 4
  • 20
  • 30
  • I haven't used Eclipse, but are you sure that your code is working fine. If by chance, it is crashing at line 'CommandConverter a(argv[1]);', and you are not notified of the crash, you will not see any output. – Vikram.exe Jan 04 '11 at 08:59
  • what happens if you run on the command line? – Nim Jan 04 '11 at 08:59
  • I'm running it atm only on commandline... but doing this through eclipse console... should be same isn't it? - Code doesn't crash there... it does a lot later... for further analysis I need the out put. If I debug and pass that point simply nothing gets displayed :/ – Beasly Jan 04 '11 at 09:04
  • try changing the `cout` to `cerr`, see if you see the output then. – Nim Jan 04 '11 at 09:08
  • same... nothing happens... i was googleing one guy had the same. His solution was use another eclipse version, I tried this too, without success :/ – Beasly Jan 04 '11 at 09:11
  • And if you run this in gdb, do you get past the `cout` statements? – Nim Jan 04 '11 at 09:15
  • Does this answer your question? [std::cout won't print](https://stackoverflow.com/questions/14858262/stdcout-wont-print) – starball Aug 30 '23 at 20:08

7 Answers7

12

You need to end output strings with newline, e.g.: `cout << "test\n"``. The reason is that the standard output is buffered and the buffer is flushed on newline. There probably exists a way to flush the cout buffer without outputting a newline, but I don't know it by heart. Probably includes access to the underlying streambuf (via the rdbuf method).

zvrba
  • 24,186
  • 3
  • 55
  • 65
  • 5
    did you try with std::endl as Ismail says? That is actually supposed to fluch the output buffer. '\n' is not. – stijn Jan 04 '11 at 09:11
  • ok with this it works... but it still doesn't make sense to me... why is it working before? On: cout << "ERROR: Wrong amount of arguments! Only one allowed...\n"; – Beasly Jan 04 '11 at 09:22
  • 2
    @Beasly, because the output is flushed to the OS, and the when your program exits, the OS takes care of flushing the buffers (i.e. display to the console). The reason the `endl` *fixes* your problem is that as well as flushing to the OS, the OS buffers *may* get flushed, reason I asked about stepping through in gdb is that relying on `cout`s for debugging is a fairly basic approach and prone to situations such as the one you encountered, stepping through with a debugger is a better approach to determining whether something is working or not... – Nim Jan 04 '11 at 10:10
  • Thanks for explaining, things get clearer now :) Actually I am debugging... but on some point I wasn't sure if some steps get really passed, thats why I want to put some couts on top of debugging :) – Beasly Jan 04 '11 at 10:18
  • 1
    "There probably exists a way to flush the cout buffer without outputting a newline, but I don't know it by heart." => [`<< flush`](http://en.cppreference.com/w/cpp/io/manip/flush) (not hard to remember really) – gx_ Jun 28 '13 at 08:08
  • Worked for me like charm. Thanks! – Yehonatan Jul 13 '14 at 06:53
  • Doesn't work for me, I also called explicitly for `flush()`, and it still doesn't print anything – hudac Sep 09 '15 at 08:23
3

For me installing the 32 bit versions of Eclipse (Indigo 3.7) and the 32 bit Java JDK/JRE did not work. I use the much quicker solution from the Eclipse CDT/User/FAQ:

Quote from Eclipse CDT/User/FAQ - Eclipse console does not show output on Windows:

Eclipse console does not show output on Windows In Eclipse CDT on Windows, standard output of the program being run or debugged is fully buffered, because it is not connected to a Windwos console, but to a pipe. See bug 173732 for more details. Either add fflush calls after every printf or add the following lines in the start of the main function:

setvbuf(stdout, NULL, _IONBF, 0); 
setvbuf(stderr, NULL, _IONBF, 0);
trenki
  • 7,133
  • 7
  • 49
  • 61
  • 1
    This Windows workaround also works for Eclipse 3.8 64-bit with CDT 6.0 on Ubuntu 12.10 64-bit. – Lucas Aug 24 '13 at 19:17
2

I had a similar problem. In my case the program would give output if run from the command line but not from eclipse console. The solution was to use the 32 bit version of eclipse and not the 64 bit one.

I read that it was a bug. Might not be the same issue though.

Lachlan
  • 21
  • 1
0

This Happens when you debug your code and dont see the output till the last. use

cout<<"what ever overloads"<< flush;

to see the output immediately on stdout(console)

falsetru
  • 357,413
  • 63
  • 732
  • 636
Puneet Singh
  • 23
  • 1
  • 3
0

I was also searching for exactly this information when I found this on the Microsoft website http://support.microsoft.com/kb/94227

I think a simple method is to use std::flush when you want to force flushing the internal buffer that cout uses

*std::cout << ... << std::flush;*
javed
  • 427
  • 1
  • 5
  • 14
-2

Hi after some similar struggle I figured out, that the first element of the project's properties environment PATH variable must be "C:\MinGW\bin;" Otherwise a wrong version might be used, especially if you use different compiler.

somebody
  • 29
  • 3
-5

try outputting a space at the beginning of each line

cout << " " << .....