Solved

Single Most Efficient Way to Clear Keyboard Buffer C++

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

943 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

10 Experts available now in Live!

Get 1:1 Help Now