View Single Post
pichlo's Avatar
Posts: 6,445 | Thanked: 20,981 times | Joined on Sep 2012 @ UK
#6
Incidentally, for line-based input like yours, the best practice is to always read a line at a time using standard functions like getline() that consume the terminating '\n', then parsing the string, as opposed to parsing the standard input directly. That way you not only avoid surprises of the "'\n' left over in the buffer" kind, but also get extra robustness for cases when the user replies with "3, you silly old program" to queries requesting only a number (hint: cin << number will only swallow the 3, the rest including the comma will be left in the input buffer for the next cin operation to read).
 

The Following 3 Users Say Thank You to pichlo For This Useful Post: