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

Flush IO Stream

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 ?
0
x25
Asked:
x25
  • 2
  • 2
  • 2
1 Solution
 
efnCommented:
You could use istream::ignore.

http://www.cplusplus.com/ref/iostream/istream/ignore.html

For example:

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

This would discard up to 99999 characters from the cin stream up to and including the next newline.
0
 
x25Author Commented:
i remember using bdflush() in C. is there anything similar ?
0
 
efnCommented:
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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
x25Author Commented:
cin.ignore() works fine.
0
 
otoolesCommented:
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
0
 
otoolesCommented:
Sorry, wrong question!! :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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