std::cout won't print
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();
C++ cout will not print
std::cout
by default is buffered. Without an explicit flush, you won't see anything printed until the internal buffers need to be flushed. This is done for performance reasons.
You can add a specific flush, like so:std::cout<<"IT WILL NOT PRINT!!!!!" << std::endl;
That said, you're not seeing output because your program is crashing.
unsigned long int fib[4000000];
is going to require nearly 15MB of space (on a 32bit long int platform). There simply won't be enough stack space to allocate such a large block of memory within that storage duration.
For such large blocks you will need to dynamically allocate the block, or better yet:
std::vector<unsigned long int> fib(4000000);
Problems with cout c++
You may need to explicitly flush the output stream or print a newline which may flush the buffer on some streams:
std::cout << std::flush;
std::cout << std::endl;
cout does not print even with time delay
std::cout
is usually buffered, meaning it may not output immediately unless you force it to. Try std::flush
after your first output:
std::cout << "hello " << std::flush;
Why doesn't the cout command print a message?
Two things to note:
First, you are not forcing the buffer to flush, so there is no guarantee the output is being sent to the screen before the program ends. Change your cout
statement to:
cout << x << endl;
Second, Visual Studio will close the console when it ends (in Debugging mode). If you do not debug it (Ctrl-F5 by default), it will keep the console open until you press a key. This will allow you to see the output. Alternatively, you can add a cin.get()
before your return statement which will force the program to wait for a character to be in the input stream before the program is allowed to exit.
Cout will not print my string
If your cities are numbered 1, 2, 3, then printcities
will be a string containing three characters, of value '\0x01' '\0x02'
and '\0x03'
. This won't print well. If you were trying to get printcities to hold "123", you either need a stringstream, or std::to_string().
Related Topics
Representing 128-Bit Numbers in C++
Constexpr If and Static_Assert
Assigning Parsers to Auto Variables
Standard Library Sort and User Defined Types
What Is the Worst Real-World Macros/Pre-Processor Abuse You'Ve Ever Come Across
Using Scanf() in C++ Programs Is Faster Than Using Cin
Confused When Boost::Asio::Io_Service Run Method Blocks/Unblocks
Why Is 'Std::Move' Named 'Std::Move'
Where to Put Default Parameter Value in C++
What Does a Colon Following a C++ Constructor Name Do
Why Should I Declare a Virtual Destructor For an Abstract Class in C++