Solved

Clearing the Input Stream

Posted on 1998-05-30
7
286 Views
Last Modified: 2010-04-02
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
Comment
Question by:strider031598
7 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1164971
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
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1164972
fflush(stdin);
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164973
What's wrong with my suggestion?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:VEngineer
ID: 1164974
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
 
LVL 11

Expert Comment

by:alexo
ID: 1164975
>> Last 10 Grades Given
>> C D D

0
 

Author Comment

by:strider031598
ID: 1164976
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
 
LVL 11

Expert Comment

by:alexo
ID: 1164977
>> Last 10 Grades Given
>> C C C D D

Some people never learn...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

13 Experts available now in Live!

Get 1:1 Help Now