Skip to main content
gamedev.net
🔒 Locked

cout vs. printf?

Started by ryanc 3 replies 1.5k views
Original Post
ryanc
ryanc
I recently discovered that when I used cout and had a pausing statement after, the text would display after the pause was over. Here's the code that does this:
    
//     Source one

#include <iostream.h>
#include <conio.h>

int main()
{
     cout<<"Hello\n";
     while(!kbhit());
     return 0;
}

//Source two

#include <stdio.h>
#include <conio.h>

int main()
{
     printf("Hello\n");
     while(!kbhit());
     return 0;
}        
Source one will wait for a key, and then display "Hello", instead of doing this backwards. Source two uses printf instead of cout and it works the way I want it to. Now, here's my question. Is there any way that I can use cout instead of printf and it not do the above? I don't mind using the printf function to print text only, but it gets complicated when you use variables and you need to print them to the screen. Cout is just more convienent. Thanks for any help. RyanC --------------------------- Games are only as good as you make them... Edited by - ryanc on 10/24/00 7:03:27 PM
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow
Shikaa
Shikaa

Yeah, you need to flush the stream buffer.

In your example you could just use

cout << "Hello" << endl;

as endl acts as the new line character and will flush the stream.

or

cout << "Hello\n";
cout.flush();

ryanc
ryanc
hey thanks Shikaa, the cout.flush() worked out well. I knew that the thing needed to be flushed or something like that, but I just didn''t know how. That really helps me out a lot. Thanks again.

RyanC
---------------------------
Games are only as good as you make them...
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow
Zipster
Zipster
cout and cin really have a lot more power than printf, even though most newbie C++ programmers just see cout as another form of printf, which is ok in the beggining.

I know this related NOTHING to your problem, but i felt like saying it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.