Can you use cout and the iostream header file with OpenGL?

I mainly use only C++ keywords when coding with OpenGL because that’s what they taught us in college. Anyway, I’ve noticed that most OpenGL source code has printf to print out program output. I was wondering if I could use cout instead since I am more accustomed to using it? I’m asking this because I don’t think I’ve ever seen it used in OpenGL source code from the web. Does it not work under OpenGL?

cout is c++, as you know, so not too sure what you mean when you say ‘work under OpenGL’.

Sorry for being unspecific in my previous question. What I mean is that most people use printf to print out let’s say the FPS of an OpenGL program. I tried doing something similar like this with cout in a program but couldn’t find the ouput line. So it made me wonder if cout could be used to shouw output since most people use printf and not cout in the OpenGL programs I’ve seen. Maybe I’ll just try it again. But, thanks for the reply.

Which you use depends on what you mean when you want to display something. To display it in the window through opengl people use some custom defined glPrint(…) routine that has the same syntax as printf, you could wrap it into a cout style stream if you wanted
if you mean writing to the console you can use either printf or cout, though I find that cout has delayed evaluation, printf actually writes at the time of the function call.
Thats just my experiance

though I find that cout has delayed evaluation, printf actually writes at the time of the function call

cout is normally a buffered output, and will flush the buffer on a new line (endl or ’
'), or when you explicitly call the flush metod on the stream. Maybe that’s what you experienced.

Originally posted by vgohan:
I mainly use only C++ keywords when coding with OpenGL because that’s what they taught us in college. Anyway, I’ve noticed that most OpenGL source code has printf to print out program output. I was wondering if I could use cout instead since I am more accustomed to using it? I’m asking this because I don’t think I’ve ever seen it used in OpenGL source code from the web. Does it not work under OpenGL?

iostream, stdio.h… have nothing to do with OpenGL. Use it whenever you like, although mixing iostream function and stdio.h is not a very good idea.

If you want cout to write text to the GL viewport, yes, you can easily do that, if you write your own ostream derived class to handle it and redirect cout to use your class. You can do the same with cerr.

I think the answer to this question is simply, “Yes, you can use cout with opengl.” Just compile with a C++ compiler.

[This message has been edited by nickels (edited 06-18-2002).]