cin input type crash

Hi.
When I run the following code and input a non-integer like a char or a floating point number (chars are integer compliant, after all, they're just ASCII representations for their respective numerical values) the console window's content start flickering and ignoring input.

I guess this is due to the cin-stream already containing a value and therefore not giving the player enough time to input anything.

Any suggestions on how to solve this, and what is the cause?

Thanks,
Per Henrik

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma region Include
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
#pragma endregion

#pragma region Entry point
int main()
{
	#pragma region Declarations
	char running = 'Y';
	int correctNumber = 25;
	int guessedNumber;
	#pragma endregion

	#pragma region Main loop
	while (running == 'Y')
	{
		// Prompt the player to enter a number and take input.
		cout << "Guess my number!" << endl;
		cin >> guessedNumber;

		// Clear the screen.
		system("CLS");

		// As the player is most likely to enter an incorrect number start checking whether it is.
		if (guessedNumber < correctNumber)
			cout << guessedNumber << " is too low." << endl;
		else if (guessedNumber > correctNumber)
			cout << guessedNumber << " is too high." << endl;
		// If the player has won the game, ask wheter he/she wants to play again and take input.
		else
		{
			cout << guessedNumber << " is correct!\nCongratulations!" << endl
				  << "Do you want to play again? (Y/N)" << endl;
			cin >> running;
			system("CLS");
		}
	}
	#pragma endregion

	return 0;
}
#pragma endregion 
closed account (z05DSL3A)
After 'cin >> guessedNumber;' put the following two lines

1
2
cin.clear();
cin.ignore();


This will clear the error flags and flush the input stream.
Thanks, exactly what I was looking for.
I better read through the documentation the next time.
Topic archived. No new replies allowed.