0
任何想法爲什麼發生這種情況?如何解決在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
這不是你的問題的原因,它只是一個毫無意義的傳遞參考。通過ref傳遞'int'比傳遞值更昂貴。如果你需要修改它,你會這樣做,但你不這樣做。 – StoryTeller