Solved

Single Most Efficient Way to Clear Keyboard Buffer C++

Posted on 2014-02-22
2
453 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
static class 3 53
tripleUp challenge 7 67
count7 challenge 12 70
Python 2.7 - French characters 6 47
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

19 Experts available now in Live!

Get 1:1 Help Now