Jump to content
  • Advertisement
Sign in to follow this  

C++: No output from cout object

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using Visual C++ 2008 Express Edition, and I am new to it. Just to test the water, I wrote a simple Windows Form application. I also use cout object to output some strings. When I debugged it in IDE, I saw no string being outputted to the debug window of the IDE. When I ran the application from command windows, I didn't see any outputs in the command windows as well. So where the strings go? Any thought? Thank you! The codes are very simple: #include <iostream> using namespace std; void CTest::Dump (void) { cout << "\r\n Test the water!"; }

Share this post


Link to post
Share on other sites
Advertisement
By default std::cout outputs to the standard output handle. However, an application running in the Windows subsystem doesn't have standard out hooked up to anything. You could create a console with AllocConsole() and then redirect standard out to that console window. Ex:

AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);

Share this post


Link to post
Share on other sites
To expand on rozz' post: Most likely, your strings are caught in some stream buffer which is not being flushed at exit of your program.

Using std::flush flushes these buffers explicitly. Outputting std::endl flushes the buffers implicitly (and creates a linebreak).

Finally, note that "\r\n" is non-standard. If your streams are opened in text mode (which they should be), a single "\n" will do the job, though it is more idiomatic to use std::endl.

Share this post


Link to post
Share on other sites
Thank you fopr your help. Here aqre some updates.

1) Method 1 works.
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf ("\r\n Test");


2) Method 2 passed the compilation, but still no output
OutputDebugString (_T("\r\nTest"));

Share this post


Link to post
Share on other sites
Quote:
Original post by Prefect
To expand on rozz' post: Most likely, your strings are caught in some stream buffer which is not being flushed at exit of your program.

Using std::flush flushes these buffers explicitly. Outputting std::endl flushes the buffers implicitly (and creates a linebreak).

Finally, note that "\r\n" is non-standard. If your streams are opened in text mode (which they should be), a single "\n" will do the job, though it is more idiomatic to use std::endl.


I tried both std::endl and std::flush, and one of them could make the outputs visible. Thanks anyway.

Share this post


Link to post
Share on other sites
I wonder though, why would the program exit so abruptly that flushing is necessary. If it is for debugging, then you could use std::cerr which should flush each output automatically.

Quote:

though it is more idiomatic to use std::endl


Is it, seeing that is doing more than is usually needed (flushing the stream) and is more to type?

Share this post


Link to post
Share on other sites
You need a debugger attched if you want to see output from OutputDebugString - did you start the program in debug mode from the IDE?

Share this post


Link to post
Share on other sites
Quote:
Is it, seeing that is doing more than is usually needed (flushing the stream) and is more to type?


I would say more idiomatic yes, efficient no.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!