Link to home
Start Free TrialLog in
Avatar of elpredicador
elpredicador

asked on

cout vs printf

I have this code that works perfectly:

#include <stdio.h>
#include <time.h>
#include <windows.h>

main(){

     time_t t;
     unsigned long mil=1000, Events;
     HANDLE hInput=GetStdHandle(STD_INPUT_HANDLE);
     INPUT_RECORD InputRecord;

     while(1){
          t=time(NULL);
          printf(asctime(localtime(&t)));
          Sleep(mil);
          system("cls");
          if(WaitForSingleObject(hInput,0)==WAIT_OBJECT_0)
          {
               ReadConsoleInput( hInput, &InputRecord, 1, &Events );
          }
          if(InputRecord.Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE){
               exit(0);
               }

     }

     return 0;
}

This prints the hour and waits 1 second before clearing the screen and printing it again. However, if I translate the printf statement to cout:
cout<<asctime(localtime(&t);
It sometimes prints intermitently, or sometimes it doesn't even bother to print. Why is this, can I avoid it (using cout of course)?
I'm using VC++6 on a XP machine
Avatar of antonsigur
antonsigur
Flag of Iceland image

You mabe need to flush the cout stream...
so it will be printed...

try
cout.flush(); after the printout
Avatar of substand
substand

also, make sure the << operator is defined for the type asctime(...) returns.
>> cout<<asctime(localtime(&t);

try this :
   cout << asctime(localtime(&t)) << endl;

i have tried this and it works. hope this help.
Avatar of Kyle Abrahams, PMP
If printf() works, then you can use that.  
ged- i think he's trying not to use c code, but c++, this being the c++ thread and all.
Avatar of elpredicador

ASKER

It's like c code is more efficient, but c++ code is mor comfortable... now I'm trying to see what's the problem with cout.
I've got two solutions to the problem, but got no explanation
The difference between printf and cout is that cout is a buffered stream. Programmers use buffered streams because they allow multiple pieces of data to be read from an input device / written to an output device at once, instead of having to access the I/O device every time. The buffer stores, in memory, the data from a particular device. The problem with this is that if the data from the device changes, the buffer won't necessarily reflect that. Buffered streams have to be refreshed, or 'flushed', to update the buffer with the new data.
ASKER CERTIFIED SOLUTION
Avatar of gotenks
gotenks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cout uses buffer... as I said, you must flush it so it will print on the screen... so cout.flush(); will flush the cout buffer onto the console screen.

printf() prints directly to the screen... no buffer...

the endl tag flushes the cout stream too... but you won't need it, you can just flush with cout.fluhsh(); clear?
Thanks all of you for answering, gotenks gets the points for giving the most complete answer.