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_IN PUT_HANDLE );
INPUT_RECORD InputRecord;
while(1){
t=time(NULL);
printf(asctime(localtime(& t)));
Sleep(mil);
system("cls");
if(WaitForSingleObject(hIn put,0)==WA IT_OBJECT_ 0)
{
ReadConsoleInput( hInput, &InputRecord, 1, &Events );
}
if(InputRecord.Event.KeyEv ent.wVirtu alKeyCode= =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
#include <stdio.h>
#include <time.h>
#include <windows.h>
main(){
time_t t;
unsigned long mil=1000, Events;
HANDLE hInput=GetStdHandle(STD_IN
INPUT_RECORD InputRecord;
while(1){
t=time(NULL);
printf(asctime(localtime(&
Sleep(mil);
system("cls");
if(WaitForSingleObject(hIn
{
ReadConsoleInput( hInput, &InputRecord, 1, &Events );
}
if(InputRecord.Event.KeyEv
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
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.
try this :
cout << asctime(localtime(&t)) << endl;
i have tried this and it works. hope this help.
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
ASKER
Thanks all of you for answering, gotenks gets the points for giving the most complete answer.
so it will be printed...
try
cout.flush(); after the printout