[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Flush IO Stream

Posted on 2005-04-28
6
Medium Priority
?
848 Views
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 ?
0
Comment
Question by:x25
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
efn earned 120 total points
ID: 13891479
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
 

Author Comment

by:x25
ID: 13891511
i remember using bdflush() in C. is there anything similar ?
0
 
LVL 15

Expert Comment

by:efn
ID: 13891704
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:x25
ID: 13891730
cin.ignore() works fine.
0
 

Expert Comment

by:otooles
ID: 14879232
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
 

Expert Comment

by:otooles
ID: 14879237
Sorry, wrong question!! :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

834 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