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

Clearing the Input Stream

How do you clear the input stream?  For example, with a statement like x = getche ( ); the input is assigned to
x, but the input is also left in the input stream.  If the following statement was cin >> y; the value that was
originally intended for x would still be in the stream and thus would be assigned to y.  How do you prevent that?
0
strider031598
Asked:
strider031598
1 Solution
 
alexoCommented:
That's what the istream::ignore() method is for.

istream& ignore( int nCount = 1, int delim = EOF );
 
nCount - The maximum number of characters to extract.
delim - The delimiter character (defaults to EOF).

Extracts and discards up to nCount characters. Extraction stops if the delimiter delim is extracted or the end of file is reached. If delim = EOF (the default), then only the end of file condition causes termination. The delimiter character is extracted.

So, in your case, you could do cin.ignore(1) to skip one character.

0
 
chensuCommented:
fflush(stdin);
0
 
alexoCommented:
What's wrong with my suggestion?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
VEngineerCommented:
I'm not sure what you are trying to do exactly, but consider using the cin.get() function from iostream.h instead of getch from conio.h
0
 
alexoCommented:
>> Last 10 Grades Given
>> C D D

0
 
strider031598Author Commented:
I can use cin.get ( ), but what I want is to use x = getche ( ) so that the user doesn't have to press enter after the input.
0
 
alexoCommented:
>> Last 10 Grades Given
>> C C C D D

Some people never learn...
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

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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