473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++ flush input buffer correctly, not a question

This is one of those extremely simple problems that no one knows how to do properly it seems. The most effective and efficient way to do this is to call ignore with the exact number of characters in the input buffer. It is sad to see people being called noobs from others who obviously have not taken the time to discover the best answer when its lying under their noses. Not that flushing the cin buffer is a big deal, but why bother acting elitist if you don't actually know what you are doing, I'd rather have a noob then someone who codes with bad habits, just saying.

cin.clear(); //clear errors/bad flags on cin

cin.ignore(cin.rdbuf()->in_avail(), '\n');//precise //amount of ignoring

cin.rdbuf()->in_avail() returns the exact number of characters in the cin buffer.

This is the best answer for a few reasons:

No clunky includes.
No giant number processing.
Required operations closer to the input buffer (in_avail vs. numeric_limits).
Consistency in following input operations (wont read past the "delimiter(\n)", or leave characters in the buffer).
Recommended by a c++ language lawyer, Dr Robert Johnson.
May 6 '16 #1
0 1165

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sam | last post by:
Good day! I have a problem with Python and the input buffer. My application must read a barcode from PS2 and then process it. The problem is: i have to discard all characters inserted in...
8
by: b83503104 | last post by:
Hi, I want to accept the user's answer yes or no. If I do this: answer = input('y or n?') and type y on the keyboard, python complains Traceback (most recent call last):
6
by: Carol Pedder | last post by:
Hello all, may I ask a question? Having just started C++ (using microsoft visual studio), I am using cin/cout for console applicarions (yes I know it doesn't happen in the real world!) and I'm...
2
by: Sharon | last post by:
At the TCP server side I'm using: /////////////////////////////////////////////////////////////////////////////// IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1"); IPEndPoint ipNport =...
1
by: r0main | last post by:
Hi, I'm facing an issue to send data throught the HttpWebRequest processing, I'm using the Method POST with a multipart form/data. While writing a file, my application is buffering the whole...
2
by: wizofaus | last post by:
Hi, If I write the following unmanaged C program: main() { char buffer; gets(buffer); }
6
by: ppuniversal | last post by:
Can anyone tell me how to flush a socket stream in C++. I am making an Client Server application in C++ and use the send() and recv() functions. i want to flush the socket buffer as i am getting...
7
by: Christopher Pisz | last post by:
I found an article http://spec.winprog.org/streams/ as a starting point, but it seems to do alot of things that aren't very standard at all. One particular problem is, that he is using a vector as...
15
by: prasath342002 | last post by:
Hi , I am having the NCLOB field in stored procedure , when i tried in one machine it is executing but in some other machine it is not executing. getting the below error ORA-22921: length of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.