• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

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
0
elpredicador
Asked:
elpredicador
  • 2
  • 2
  • 2
  • +3
1 Solution
 
antonsigurCommented:
You mabe need to flush the cout stream...
so it will be printed...

try
cout.flush(); after the printout
0
 
substandCommented:
also, make sure the << operator is defined for the type asctime(...) returns.
0
 
gotenksCommented:
>> cout<<asctime(localtime(&t);

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

i have tried this and it works. hope this help.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kyle AbrahamsSenior .Net DeveloperCommented:
If printf() works, then you can use that.  
0
 
substandCommented:
ged- i think he's trying not to use c code, but c++, this being the c++ thread and all.
0
 
elpredicadorAuthor Commented:
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
0
 
DarthNemesisCommented:
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.
0
 
gotenksCommented:
basically, cout.flush() and endl will output or flush the buffer to the console or output stream. therefore, if within your code, you don't have any line that contains the cout.flush() or the cout << endl, the buffer will just keep appending the output, without flushing it out to the screen.
for example, if you put a line 'cout << endl;' inside one of your if-statement (within the while loop, let say the statement that check for VK_ESCAPE), and press the escape key after some time (let say 10 seconds later), you will see 10 lines of output.

main(){
    ...
    while(1){
        ...
        cout << asctime(localtime(&t));
        ...
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE){
            cout << endl; // or cout.flush();
            exit(0);
              }

    }

    return 0;
}

hope, i have explained that correctly.
0
 
antonsigurCommented:
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?
0
 
elpredicadorAuthor Commented:
Thanks all of you for answering, gotenks gets the points for giving the most complete answer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now