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?
#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;
elseif (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