所以我正在为一个类编写一个程序,并在函数定义中设置一个if循环,为条目设置参数。我应该只接受0到10之间的输入。但它只捕捉小于0的数字。它不会捕捉到大于10的数字。
int main()
{
float score1, score2, score3, score4, score5;
cout << endl;
cout << "Judge #1: " << endl;
getJudgeData(score1);
cout << "Judge #2: " << endl;
getJudgeData(score2);
cout << "Judge #3: " << endl;
getJudgeData(score3);
cout << "Score #4: " << endl;
getJudgeData(score4);
cout << "Score #5: " << endl;
getJudgeData(score5);
calcScore(score1, score2, score3, score4, score5);
return 0;
}
void getJudgeData (float &score)
{
cin >> score;
if(score < 0 || score > 10)
{
cout << "Error: Please enter a score between 0 and 10." << endl;
cin >> score;
}
}
请将getJudgeData
函数中的if
条件更改为while
循环:
void getJudgeData (float &score)
{
cin >> score;
while (score < 0 || score > 10)
{
cout << "Error: Please enter a score between 0 and 10." << endl;
cin >> score;
}
}
否则,条件将只检查一次,这意味着每个法官的第一次输入。如果我正确理解你的问题,这不是故意的。
请在此处查找有关while
循环的更多信息:
而