Troubles with cout
Hello...I''m a beginning to intermedeate C++ programmer, and recently I decided to switch all the printf''s in my program to cout to try and move it more to C++ rather than the C standard library. However, I soon encountered a problem. For some reason, anything I print via cout does not show up until main() returns. I''m quite sure I''m using cout correctly...here''s a tiny sample program of what I''m talking about:
#include "iostream.h"
#include "windows.h"
void main()
{
cout << "This doesn''t work\n";
Sleep(3000);
}
If HTML eats it, there are two less than signs between cout and the quote.
Assuming this runs the same for you as it does for me, the text will not display until after the 3 second sleep. In my actual program, I don''t have sleep, I have a while loop and some other misc. functions that should be putting out messages. However, none of it shows up until the end of the program when ALL the text scrolls by at once and then it shuts down.
Does anybody have ANY idea why this should happen? When I replace them with printf again it works fine; it''s just cout. I am using MS Visual C++ 6 if it makes a difference, it''s a Win32 Console App.
Any help would be appreciated, since I happen to like cout more than printf when it works!
Anthracks
Sometimes, text output is buffered in memory and not drawn to the screen until a newline is reached. Add an endl to your code and it should work:
I hope that helps!
---
Oh, sorry. I didn't see that you have a \n in your string already when I posted my response. That seems really odd that it isn't getting displayed. If using endl doesn't work, maybe there's something that is equivalent to fflush() that can be used with iostreams?
Edited by - jaxson on May 26, 2000 4:30:41 PM
cout << "Hi" << endl;
I hope that helps!
---
Oh, sorry. I didn't see that you have a \n in your string already when I posted my response. That seems really odd that it isn't getting displayed. If using endl doesn't work, maybe there's something that is equivalent to fflush() that can be used with iostreams?
Edited by - jaxson on May 26, 2000 4:30:41 PM
ok some usefull tips. c++ doest require that you return anything so if you arnt returning any thing dont put retun in you prog.
cout << "\n\n\tthis should work\n"
<< "and goes on...";
and another thing dont use the dalay command at all! its for dos not windowz!!!! this may be the root of the prob.
but nice to know if i help u say so if we have
~prevail by daring to fail~
cout << "\n\n\tthis should work\n"
<< "and goes on...";
and another thing dont use the dalay command at all! its for dos not windowz!!!! this may be the root of the prob.
but nice to know if i help u say so if we have
~prevail by daring to fail~
Thanks, cout << endl; did the trick. One last question I guess would be, do I HAVE to do this? It is more than a little annoying to remember to do an << endl at the end of every block of text. I can live with it, but it''s something extra to remember and seems a bit unnecessary. Ah well, if it made sense it wouldn''t be programming.
Anthracks
Anthracks
cout is buffered, so output won''t show immediately. You can either use flush() to explicitly flush any ostream (cout is an ostream object), or you can use endl. endl is not the same as putting "\n" in the string to be displayed! endl puts a newline into the stream, and then flushes it as well.
Btw, there''s cerr and clog as well. cerr is the error output (stderr when using fprintf etc), and clog is an extra object that provides unbuffered output, if I recall correctly. clog is easy to redirect for example debug output to a file. This can be done by:
ofstream file( "log.txt" );
streambuf* old_buf = clog.rdbuf( file.rdbuf() );
// do stuff
...
// restore clog
clog.rdbuf( old_buf);
Erik
Btw, there''s cerr and clog as well. cerr is the error output (stderr when using fprintf etc), and clog is an extra object that provides unbuffered output, if I recall correctly. clog is easy to redirect for example debug output to a file. This can be done by:
ofstream file( "log.txt" );
streambuf* old_buf = clog.rdbuf( file.rdbuf() );
// do stuff
...
// restore clog
clog.rdbuf( old_buf);
Erik
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement