News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ tips: IO - Discarding characters using cin

Started by ben2ong2, October 06, 2006, 11:04:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Robert Charles Fellenz)

If the following statements are in my program, how do I flush the
the "cin" buffer if a user inputs more than ten integers (separated
by spaces) for the loop with the "cin" statement.

[ Sorry, I missed the original code. But it is not needed to
  understand the response below. -adc ]



RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 25 Sep 1992

Use the function
   istream::ignore(int count, int delim=EOF)

It extracts and discards characters until
- 'count' characters are extracted,
- the character 'delim' is encountered,
- the end of file is reached,
whichever occurs first.  If 'delim' is EOF (the default), exactly
'count' characters will be discarded (unless end of file is reached).

To discard the remainder of a line, use
   cin.ignore(MANY, '\n');
where 'MANY' is INT_MAX is from <limits.h>, or is some positive number
that fits in an int and is bigger than the length of any line.


You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login