Scriptdaemon
11-17-2007, 02:40 AM
Example:
cout << "Enter a year to display the first month of that year: ";
cin >> year;
while(year < 1 || !cin.good())
{
cout << endl << "Invalid year!" << endl
<< "Enter a year to display the first month of that year: ";
cin >> year;
}
It asks me to enter the year, and I do. If it's not a number, it just runs the loop over and over. I've used it in an if statement to check if a user enters letters instead of numbers before and it checked a value and returned an error as I expected, but in a while loop, it doesn't work as I expected, and runs the loop over and over without prompting me to re-enter the year, how can I fix this?
cout << "Enter a year to display the first month of that year: ";
cin >> year;
while(year < 1 || !cin.good())
{
cout << endl << "Invalid year!" << endl
<< "Enter a year to display the first month of that year: ";
cin >> year;
}
It asks me to enter the year, and I do. If it's not a number, it just runs the loop over and over. I've used it in an if statement to check if a user enters letters instead of numbers before and it checked a value and returned an error as I expected, but in a while loop, it doesn't work as I expected, and runs the loop over and over without prompting me to re-enter the year, how can I fix this?