#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”,而最佳功能始终会选择最高分数的名字。我试图将测试成绩设置为其他数组,但这似乎无济于事。你能帮忙的话,我会很高兴。谢谢!

最佳答案

如果student[i]87,它将满足第二个条件并获得'B'等级,但随后它也将失败第四个条件并进入else,得到'F'等级,该等级将覆盖'B'
只有第四个ifelse关联。以上三个是独立的,因此将不必要地进行检查。

                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与所有先前的条件相关联,因此,如果以上任何条件为true,则else将不会执行。

关于c++ - struct int数据类型不适用于关系运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63163672/

10-15 07:01