I'll start by putting up the functions in question:
int play() // asks user if they want to play
{
cout << "Would you like to play a game of dice with me?\n1.Yes\n2.No";
bline(1); //writes blank lines
check();
return ans;
}
int playagn() // asks user if they want to play again
{
cout << "Would you like to play again? Please!\n1.Yes\n2.No";
bline(1);
check();
return ans;
}
void check() //checks if imput is valid
{
cin >> ans;
while(ans!=1 && ans!=2)
{
cout << "Please answer with either 1 or 2";
while(!cin >> ans)
{
cin.clear();
cin.ignore(std::numeric_limits < int >::max(), '\n');
}
bline(1);
cin >> ans;
bline(1);
}
}
I took the middle part in check() from the faq, because I was having a problem with infinite looping when the imput was a char and int was expected.
It works, but only for play(). If I put a char in during playagn(), it goes into an infinite loop, even though the two functions are nearly identical and both use check().
What's going on here?