Solved

Single Most Efficient Way to Clear Keyboard Buffer C++

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
copyEvens challenge 6 99
My project did see openJDK that I installed. What could be the problem 7 143
How can i compile this github project?? 2 85
Capture logon name 13 74
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

831 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