?
Solved

Clearing the Input Stream

Posted on 1998-05-30
7
Medium Priority
?
294 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
Independent Software Vendors: 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

650 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