Solved

Clearing the Input Stream

Posted on 1998-05-30
7
289 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 87
No module found pypyodbc, 3 33
Where are the c++ header files to use in Eclipse? 4 25
Indy 10 not Receiving UDP broadcast 3 23
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 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