Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can explain std::cout not to display anything?

For whatever reason, std::cout does not display anything with my application. The description of my development environment follows.

I am working on a Qt application using Qt Creator. Since Qt Creator can't be launched from my station (XP64), i am currently developping it with Visual Studio 2008 and the Qt plugin (by importing the .pro project file). Everything seems fine and the application works.

In some cases (depending on command line arguments), i don't want to launch the HIM, just to display a few sentences in the CLI (command line required arguments, for instance).

I don't get any error, but nothing is displayed. The corresponding code, which i am sure is run is the (classical) following :

std::cout << "is this going to be displayed ?" << std::endl;

Do you have any idea why nothing is displayed ?

like image 557
Benoît Avatar asked Apr 13 '10 08:04

Benoît


People also ask

Why is cout not printing anything?

This may happen because std::cout is writing to output buffer which is waiting to be flushed. If no flushing occurs nothing will print. So you may have to flush the buffer manually by doing the following: std::cout.

Why is cout not working C++?

There is no std::cout in C. In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.

Why can't I use cout?

You need to put it in a function. You need to #include <string> before you can use the string class and iostream before you use cout or endl . string , cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.

What does std :: cout do in C++?

One of the most useful is std::cout, which allows us to send data to the console to be printed as text. cout stands for “character output”.


1 Answers

On Windows, programs usually are built as either a SUBSYSTEM:WINDOWS application or as SUBSYSTEM:CONSOLE.

Programs built with SUBSYSTEM:CONSOLE are expected to be text-mode applications. For this type of application, stdout and stderr print to the console that you launched them from, creating a new console if necessary.

In contrast, SUBSYSTEM:WINDOWS applications do not bother with a console. You can still write to stdout and stderr, but they normally don't go anywhere. You could use AllocConsole to create a console to print to, but this will always print to a newly created console window, not to a console window you launched the program from.

One trick for SUBSYSTEM:WINDOWS applications is that even though there's no console, you can still pipe stdout and stderr. To pipe stdout, you can do:

YourApplication.exe > output.txt

or if you have cat (or an equivalent):

YourApplication.exe | cat

Also note that there's not really any difference between SUBSYSTEM:WINDOWS applications and SUBSYSTEM:CONSOLE applications other than how Windows treats them when creating the process. (You can create windows in SUBSYSTEM:CONSOLE applications.) You therefore can easily switch between SUBSYSTEM types (for example, to use SUBSYSTEM:CONSOLE for debug builds and SUBSYSTEM:WINDOWS for release ones).

like image 169
jamesdlin Avatar answered Oct 21 '22 10:10

jamesdlin