?
Solved

Clearing the Input Stream

Posted on 1998-05-30
7
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 total points
ID: 1164972
fflush(stdin);
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164973
What's wrong with my suggestion?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 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