Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ input buffer

Status
Not open for further replies.

james00harper

Programmer
May 24, 2002
4
GB
When using the c++ cin.getline function you can use an overloaded version that allows you to enter input the name of the string where the input stream will be copied to, the size of the string and the delimiting character. Eg cin.getline(Buffer,20,'\n');

Function works fine for less than 20 character. As expected when you input more than 20 characters only the first 20 characters are copied to Buffer. However the remaining characters of the stream after the 20th character are held in an input buffer. When ever you want to use cin.getline again the buffered characters are dumped on screen not allowing you to use the function unless its called again.
How do you flush the input buffer. Have already tried cin.clear, cout.flush and loads of other things.

Any advice - its a bit of a mouth full, but I'm pulling my hair out about it.
 
I was having this same problem. If you enter in the function (cin.ignore) just before your cin.getline (be sure to reference at least the same number of characters your getline function calls for), it will clear the buffer:

example:

#define nameSize 25

cout << &quot;\nEnter a name to search for: &quot;;
cin.ignore(nameSize, '\n');
cin.getline(iString, nameSize);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top