Flush IO Stream

Posted on 2005-04-28
Last Modified: 2008-02-01
i have function as follows:

void func()
   char   a;


   cout << "Enter: ";
   cin >> a;


 However, on the next iteration of cin >> a, a would have the value of '\n'
which was entered in the first place. Someone told me to flush the io stream on each iteration. How ?
Question by:x25
    LVL 15

    Accepted Solution

    You could use istream::ignore.

    For example:

    cin.ignore(99999, '\n');

    This would discard up to 99999 characters from the cin stream up to and including the next newline.

    Author Comment

    i remember using bdflush() in C. is there anything similar ?
    LVL 15

    Expert Comment

    bdflush is a Linux thing, not a C thing.  If your program is to run under Linux, you can use bdflush in C++ as well as in C.  But since it has to do with flushing buffers to disk, I don't think it would help you with a cin input stream.

    Author Comment

    cin.ignore() works fine.

    Expert Comment

    Thanks, it is now accepting the input using cin.ignore. BUT my Customer Search runs into a problem now.

    When I enter a customer number, it will find it, display the Customer No. but then the ERROR below pops up:

    An Access Violation (Segmentation Fault) Raised in your program

    Expert Comment

    Sorry, wrong question!! :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now