There are some ridiculous things in C++, like the fact that there exists getc(), getch(), getche(), getchar(), and _getch() and they’re all different. Press Enter to Continue getch

If you’re doing cin of an integer, and the user enters a character, they’ll get stuck in an infinite loop. I found a good way to fix this at bytes.com

There are several possible ways to remove the unwanted
characters. Here’s one:

cin.clear(); /* reset stream state to ‘good’ */
cin.ignore(numeric_limits<streamsize>::max(), ‘\n’);

This means: “extract and discard all characters up
to the first newline character, or the maximum possible
number of characters in a stream, whichever occurs first.”

You’ll need to add:
#include <limits> // for declaration of ‘numeric_limits’
#include <ios> // for declaration of ’streamsize’

This entry was posted on Thursday, May 1st, 2008 at 9:45 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply