Using flush command.

This is a discussion on Using flush command. within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a newbie at C++. I've made my first program in C++. It's quite small, 181 lines of ...

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Using flush command.

    Hi,

    I am a newbie at C++.
    I've made my first program in C++.
    It's quite small, 181 lines of code in fact
    I finally got it to compile and run successfully

    The thing is, I had to use the flush command very often.
    Without them, the program wasn't running well

    Here is an example of what I was using:

    cout<<"text"<<flush;

    My question is, is there a certain pattern that I must use for the flush command? In other words, when do I have to use it?

    I found that I never know exactly when to use it, in fact, I had to keep trying it in different places until it finally worked well.

    Any ideas?

    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Using cout you are dealing with buffered I/O which basically means that your code is not the thing making the decision as to when stuff you have sent to be output will actually be displayed. When you write something to the screen using cout many people would expect that the data goes right to the screen immediately. What really happens is that all of this output goes to a buffer and the data held in that buffer only gets sent to the screen once it is full. So, if you do something like asking the user for input using cout and you haven't flushed the buffer, then the user may see a blank screen because the output buffer is not full and the information you wanted to be displayed is sitting in the buffer waiting for it to get full. You can easily avoid this by outputting an endl at the end of all your cout statements since this automatically flushes the buffer along with printing out a new line.

    Code:
    cout << "Hello there!" << endl;  // Buffer flushed automatically
    Hopefully this gives you some understanding, however minimal, of what's going on and helps in some way.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 11:54 AM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21