struct int 数据类型不适用于关系运算符
struct int data type not working with relational operaters
#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 功能最好,等级似乎不起作用。当程序运行 grade 函数时,所有的 grades 'F' 和 best 函数总是选择最高分的名字。我试图将测试分数设置在不同的数组中,但这似乎没有帮助。你能帮忙的话,我会很高兴。谢谢!
如果student[i]
是87
,它将满足第二个条件并获得'B'
的成绩,但它也会失败第四个条件并进入[=15] =],获得 'F'
的成绩,这会覆盖 'B'
.
只有您的第四个 if
与 else
关联。以上三个是独立的,所以检查会不必要的。
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';
你应该使用 if-else
if (student[i].testScore >= 90)
student[i].grade = 'A';
else if (student[i].testScore >= 80)
student[i].grade = 'B';
else if (student[i].testScore >= 70)
student[i].grade = 'C';
else if (student[i].testScore >= 60)
student[i].grade = 'D';
else
student[i].grade = 'F';
如果第一个条件不满足,则不需要在第二个条件中再次检查。最后的 else
与所有先前的条件相关联,因此如果上述任何条件为真,则 else
将不会被执行。
#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 功能最好,等级似乎不起作用。当程序运行 grade 函数时,所有的 grades 'F' 和 best 函数总是选择最高分的名字。我试图将测试分数设置在不同的数组中,但这似乎没有帮助。你能帮忙的话,我会很高兴。谢谢!
如果student[i]
是87
,它将满足第二个条件并获得'B'
的成绩,但它也会失败第四个条件并进入[=15] =],获得 'F'
的成绩,这会覆盖 'B'
.
只有您的第四个 if
与 else
关联。以上三个是独立的,所以检查会不必要的。
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';
你应该使用 if-else
if (student[i].testScore >= 90)
student[i].grade = 'A';
else if (student[i].testScore >= 80)
student[i].grade = 'B';
else if (student[i].testScore >= 70)
student[i].grade = 'C';
else if (student[i].testScore >= 60)
student[i].grade = 'D';
else
student[i].grade = 'F';
如果第一个条件不满足,则不需要在第二个条件中再次检查。最后的 else
与所有先前的条件相关联,因此如果上述任何条件为真,则 else
将不会被执行。