Solved

Single Most Efficient Way to Clear Keyboard Buffer C++

Posted on 2014-02-22
2
468 Views
Last Modified: 2014-02-24
Hi All,

Can someone please tell me the single most efficient way to clear the keyboard buffer in C++?  I found the following but don't know if it's efficient:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

Some background:  I'm a college student in 1st Semester OOP.

- Thanks
0
Comment
Question by:realchick
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39879872
That's indeed the most portable way to do that, it just looks a bit complicated, while it isn't complicated at all. E.g. 'numeric_limits<streamsize>::max()' evaluates to a single value. Plus, it is way more efficiant to run a loop until 'cin()' is empty. So, apart from some specialities for some compilers/frameworks (e.g. MS compilers would allow a 'rewind(stdin);' for that), it is probably the most efficiant and simple way.
0
 
LVL 1

Author Closing Comment

by:realchick
ID: 39881966
Good, I'm going to post this to my classroom discussion.  Out textbook doesn't mention "numeric_limits" or even cin.clear()...so I struggled to find a good input validation solution online and my while loop just ran for miles and miles!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Not needed 13 120
c++, dynamic object by json 1 53
Bartender Integration Builder 3 45
what is the best portable software where i can test codes written in different languages 1 57
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

821 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