Let's say I am asking the user to enter a number of type double and they enter a char instead, why does my program go into an infinite loop? And how do I prevent this? I know it has to do something with cin.ignore()? I'm just not sure how to use it.
cout << "Score " << i << ": "; // Display the prompt
while (!(cin >> score)) // Get the input and validate it
{
cin.clear(); // Clear the error flags
cin.ignore(100, '\n'); // Remove unwanted characters from buffer
cout << "Score " << i << ": "; // Re-issue the prompt
}
The above caters for non-numeric data. You could combine it with the other test,