この回答を検索しましたが、このエラーを修正する方法を誰も知らないようです。入力を厳密にintにしたい。入力が double の場合は、エラーを送信するようにします。
int creatLegs = 0;
string trash;
bool validLegs = true;
do
{
cout << "How many legs should the creature have? ";
cin >> creatLegs;
if(cin.fail())
{
cin.clear();
cin >> trash; //sets to string, so that cin.ignore() ignores the whole string.
cin.ignore(); //only ignores one character
validLegs = false;
}
if (creatLegs > 0)
{
validLegs = true;
}
if (!validLegs)
{
cout << "Invalid value, try again.\n";
}
} while (!validLegs);
ほぼ効きそうです。エラーを送信しますが、次のループに移動した後でのみです。どうすればこれを修正できますか? そして、エラーメッセージがまだ表示されているのに、表示される前にまだ進んでいるのはなぜですか?