C++函数运行,即使它不应该运行?

C++ function runs even if it shouldn't?

本文关键字:运行 不应该 函数 C++      更新时间:2023-10-16

我是C++新手,但我对它有很好的处理。我正在尝试列出姓名和等级并找到最大的。一切都在工作,除了我的函数在每次我检查最高等级时运行。

    //Compares to find highest grade
    void highestTest(studentType info[]){
            int testValue, i;
            testValue = 0;
            i = 0;
            for(i; i < 20; i++){
                if(info[testValue].testScore < info[i].testScore){ 
                    testValue = i;
                }
            }
            //Should run at the very end of the function
            if(i == 20)
                    outPut(testValue, info);
    }
    void outPut(int highTest, studentType info[]){
            cout << "The highest test score goes to " << info[highTest].studentFName << " " << info[highTest].studetnLName << " with a grade of " << info[highTest].testScore << endl;
            cout << "~~~~~~~~~~~~~~~~~~Students~~~~~~~~~~~~~~~~~~~~~~~" << endl;
            for(int i = 0; i < 20; i++){
                    cout << info[i].studetnLName << ", " << info[i].studentFName << info[i].grade << endl;
            }
    }

代码的其余部分如下,我知道这不是最好的方法,但我不允许在 main 中包含任何其他内容,并且我们对需要什么类型的函数有具体要求。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct studentType{
    string studentFName;
    string studetnLName;
    int testScore;
    char grade;
};
void fillArray(studentType info[]);
void labelGrades(studentType info[]);
void  highestTest(studentType info[]);
void outPut(int highTest, studentType info[]);
    string first[20] = {"student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student"};
    string last[20] = {"student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student"};
    int grades[20] = {97, 98, 96, 99, 86, 94, 88, 84, 85, 86, 89, 100, 84, 97, 91, 82, 92, 83, 89, 95};
    studentType info[20];
int main() {
    fillArray(info);
    return 0;
}
void fillArray(studentType info[]){
    for(int i = 0; i < 20; i++){
        info[i].studentFName = first[i];
        info[i].studetnLName = last[i];
        info[i].testScore = grades[i];
    }
    labelGrades(info);
}
void labelGrades(studentType info[]){
    int i = 0;
    for(i; i < 20; i++){
        if(info[i].testScore >= 90 && info[i].testScore <= 100){
            info[i].grade = 'A';
            highestTest(info);
        }else if(info[i].testScore >= 80 && info[i].testScore <= 89 )
            info[i].grade = 'B';
        else if(info[i].testScore >= 70 && info[i].testScore <= 79 )
            info[i].grade = 'C';
        else if(info[i].testScore >= 60 && info[i].testScore <= 69 )
            info[i].grade = 'D';
        else if(info[i].testScore <= 59)
            info[i].grade = 'F';
        else
            cout << "An error occured please let the developer know" << endl;
    }
}

我确实将所有名字(名字/姓氏)都改为学生,以免透露同学。如果有人知道为什么每次出现"A"时都会运行 void output 以及任何可能的修复程序,那就太好了!

退出 for 循环for(i; i < 20; i++){时,i 的值将为 i == 20。 因此,测试if (i == 20)将始终通过,并且每次都会调用outPut()

i = 20 时,这 for 循环结束

for(i; i < 20; i++){
    if(info[testValue].testScore < info[i].testScore){ 
        testValue = i;
    }
}

然后你的下一行是一个测试 i == 20 的 if 语句,所以每次你调用这个函数时,它都会调用 outPut。