我是 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 以及任何可能的修复,那就太好了!