我的if语句不是100%正常工作,我不确定我做错了什么。非常感谢,谢谢!这是我的功能
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lownum, int highnum)
{
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lownum >> highnum;
if ( lownum < 0 || highnum < 0 || lownum > highnum || lownum > 100 || highnum > 100)
{
cout <<"Please re-enter 2 values of test scores that you want to view from within the range 0-100";
cin >> lownum >> highnum;
}
cout << endl << "List of students with scores in the range " << lownum << " to " << highnum << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if(student[i].testScore >= lownum && student[i].testScore <= highnum)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
}
cout << endl;
}
答案 0 :(得分:2)
不确定您所关注的内容,但是关于第一个if
语句,您可能希望将输入和验证放在循环中:
do {
values = get_input();
} while (!values_are_valid());
答案 1 :(得分:1)
我会在你的if语句中使用括号
if ( (lownum < 0) || (highnum < 0) || (lownum > highnum) || (lownum > 100) || (highnum > 100))
过去我没有这样做时,我遇到了问题。
答案 2 :(得分:0)
请说明一下 如果不合适也只是一个注释 如果第二次输入的数据不正确,您首先只能工作一次 您不会要求用户重新输入, 如果您想获得正确的输入 你应该用一段时间替换你的第一个。
但总的来说,最好使用
Do {
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lownum >> highnum;
}while( (lownum < 0) || (highnum < 0) || (lownum > highnum) || (lownum > 100) || (highnum > 100)); //statement
Do会第一次运行,然后如果输入不正确,语句将在输入正确后触发do部分。 您可以将couts编辑为“Enter / ReEnter”等。