Thread: Using flush command.

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

    Using flush command.


    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:


    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?

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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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.

    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, 12:54 PM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM