0

我的 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;
}
4

3 回答 3

2

不确定您在追求什么,但关于第一if条语句,您可能希望将输入和验证放入循环中:

do {
    values = get_input();
} while (!values_are_valid());
于 2013-11-07T03:11:00.390 回答
1

I would use parenthesis in your if statement

if ( (lownum < 0) || (highnum < 0) || (lownum > highnum) || (lownum > 100) || (highnum > 100))

I have had problems when I didn't do that in the past.

于 2013-11-07T03:15:23.717 回答
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 将按原样第一次运行,然后如果输入不正确,while 语句将在输入正确后触发 do 部分。您可以将 couts 编辑为“Enter/ReEnter”等。

于 2013-11-07T03:12:51.513 回答