Gra*_*y D 1 c++ user-defined-functions
我是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;
    }
}
我确实将所有姓名(第一个/最后一个)改为学生,以便不透露同学.如果有人知道为什么每次都有outPut运行,那么就有一个'A'和任何可能的修复,这将是伟大的!