【发布时间】:2020-11-19 15:18:55
【问题描述】:
#include <iostream>
#include <fstream>
using namespace std;
struct studentInfo {
string studentFname, studentLname;
int testScore;
char grade;
}student[20];
void inputs(studentInfo(&student)[20]) {
ifstream openFile;
openFile.open("Students.txt");
if (!openFile.is_open())
cerr << "ERROR! failed to open file\n";
for (int i = 0; i < 20; i++) {
openFile >> student[i].studentFname;
openFile >> student[i].studentLname;
openFile >> student[i].testScore;
}
openFile.close();
}
void grade(studentInfo(&student)[20]) {
for (int i = 0; i < 20; i++) {
if (student[i].testScore >= 90)
student[i].grade = 'A';
if (student[i].testScore >= 80 && student[i].testScore < 90)
student[i].grade = 'B';
if (student[i].testScore >= 70 && student[i].testScore < 80)
student[i].grade = 'C';
if (student[i].testScore >= 60 && student[i].testScore < 70)
student[i].grade = 'D';
else
student[i].grade = 'F';
}
}
void best(studentInfo(&student)[20], int index) {
int largest;
largest = 0;
for (int n = 0; n < 20; n++) {
if (largest < student[n].testScore) {
largest = student[n].testScore;
index = n;
}
}
}
void output(studentInfo(&student)[20], int n) {
ofstream outfile;
outfile.open("StudentGrade.txt");
for (int i = 0; i < 20; i++)
outfile << student[i].studentLname << ", " << student[i].studentFname << " " << student[i].testScore << " " << student[i].grade << endl;
outfile << student[n].studentFname << " " << student[n].studentLname << " has the best grade!";
outfile.close();
}
int main()
{
studentInfo Istudent[20];
int tests[20];
int large = 0;
inputs(Istudent);
for (int i = 0; i< 20; i++)
Istudent[i].testScore= tests[i];
grade(Istudent);
best(Istudent, large);
output(Istudent, large);
return 0;
}
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
Lance Norman 88
我的 void 功能最好,等级似乎不起作用。当程序运行时,等级功能使所有等级为“F”,并且最好的功能总是选择最高分的名字。我试图在不同的数组中设置测试分数,但这似乎没有帮助。你能帮忙的话,我会很高兴。谢谢!
【问题讨论】:
-
best应该把index当作int&,否则main中的large不会被修改。 -
谢谢!这解决了我的一个问题。