これはどうして起こるのでしょうか?cin.fail()のループを解決するには
void askNQ(int &noq)
{
bool x = true;
while (x)
{
int i;
cout << "How many questions would you like(out of " << noq << ")?" << endl;
cin >> i;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Sorry, that is not valid." << endl;
x = true;
}
else if (i > noq)
{
cout << "Sorry, that is too many." << endl;
x = true;
}
else if (i <= 0)
{
cout << "Sorry, that is too less." << endl;
x= true;
}
}
}
あなたは 'noq'を変更しません、どうしてこれは参照渡しですか? – StoryTeller
yah noqは参照渡しです。 noqが問題になることがありますか? – Beng970804
それはあなたの問題の原因ではありません、それはちょっと無意味な参照渡しです。 'int'をrefで渡すのは、それを渡すよりもコストがかかります。あなたはそれを変更する必要がある場合はそれを行うだろうが、そうしない。 – StoryTeller