?
Solved

cout vs printf

Posted on 2003-03-10
10
Medium Priority
?
726 Views
Last Modified: 2013-12-14
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
Comment
Question by:elpredicador
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 2

Expert Comment

by:antonsigur
ID: 8106723
You mabe need to flush the cout stream...
so it will be printed...

try
cout.flush(); after the printout
0
 
LVL 10

Expert Comment

by:substand
ID: 8107219
also, make sure the << operator is defined for the type asctime(...) returns.
0
 

Expert Comment

by:gotenks
ID: 8107425
>> 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!

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8107610
If printf() works, then you can use that.  
0
 
LVL 10

Expert Comment

by:substand
ID: 8107725
ged- i think he's trying not to use c code, but c++, this being the c++ thread and all.
0
 

Author Comment

by:elpredicador
ID: 8107870
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
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8108168
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
 

Accepted Solution

by:
gotenks earned 200 total points
ID: 8108230
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8110291
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
 

Author Comment

by:elpredicador
ID: 8131227
Thanks all of you for answering, gotenks gets the points for giving the most complete answer.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question