helios wrote: |
---|
Wow. I never realized extracting from std::cin sucked this hard. |
People have no idea how difficult proper input is.
Never just "clear the input buffer". That implies that you are simply ignoring the user for some (unspecified, irrational) reason.
The trick is to make sure your input routines are properly synchronized with the actual input.
(I'll skip the soapbox lecture about how input handling is mishandled by almost all instructional media.)
To get input from the user,
read a string with getline(). Then figure out if it is valid or not.
(Because
the user will always press ENTER at the end of every (prompted) input.)
I hate to keep recurring to this post... it's a little dated but the principles remain the same. Check out lines 29 to 41, and observed how to use it in the example
main().
http://www.cplusplus.com/forum/beginner/13044/#msg62827
Even if you don't use a fancy functor like that, it is always worth writing one or more routines that are designed to handle one specific type of input -- like getting an integer in a specified range.
http://www.cplusplus.com/forum/beginner/18258/#msg92955
(Notice that this last example suffers from the "123abc" problem, which the first does not.)
Hope this helps.