Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cout won't print

Is there any circumstance when std::cout << "hello" doesn't work? I have a c/c++ code, however the std::cout doesn't print anything, not even constant strings (such as "hello").

Is there any way to check if cout is able/unable to open the stream? There are some member functions like good(), bad(), ... but I don't know which one is suitable for me.

like image 311
mahmood Avatar asked Feb 13 '13 16:02

mahmood


People also ask

Why is std :: cout not working?

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 does C++ use cout instead of print?

cout is a object for which << operator is overloaded, which send output to standard output device. The main difference is that printf() is used to send formated string to the standard output, while cout doesn't let you do the same, if you are doing some program serious, you should be using printf().

How do you print something in C++?

Std::cout is the preferred way to print a string in C++.

Where does cout print to C++?

The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout.


2 Answers

Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will be flushed unless you manually flush it yourself.

std::cout << "Hello" << std::endl; 

std::endl will output a newline and flush the stream. Alternatively, std::flush will just do the flush. Flushing can also be done using the stream's member function:

std::cout.flush(); 
like image 111
Joseph Mansfield Avatar answered Sep 20 '22 19:09

Joseph Mansfield


std::cout won't work on GUI apps!

Specific to MS Visual Studio: When you want a console application and use MS Visual Studio, set project property "Linker -> System -> SubSystem" to Console. After creating a new Win32 project (for a native C++ app) in Visual Studio, this setting defaults to "Windows" which prevents std::cout from putting any output to the console.

like image 27
Jan Paulmann Avatar answered Sep 20 '22 19:09

Jan Paulmann