0

我正在构建的程序旨在将学生姓名、他们的 5 个分数和他们的成绩从名为“students.txt”的输入文件输出到名为“output.txt”的输出文件中。所需的所有计算都是正确的,但输出文件的格式不正确,因为我希望所有分数和成绩都在其标题下对齐。我的印象是 setw 在其中包含了它所应用的文本,但据我所知,它似乎在同一个文件中的应用不一致。我知道问题在于使用的名称长度不同,我只是不知道如何解决,因为我认为 setw 应该用于在这种情况下进行格式化。这是代码:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

struct studentType
{
    int scores[5];
    string fName;
    string lName;
    char grade;
};

void read(ifstream& inFile, studentType students[], int numStudents);

void calcGrade(studentType students[], int numStudents);

void print(ofstream& outFile, studentType students[], int numStudents);

int main()
{
    ifstream inFile;
    ofstream outFile;

    const int numStudents = 12;

    studentType students[20];

    inFile.open("students.txt");
    outFile.open("output.txt");

    read(inFile, students, numStudents);

    calcGrade(students, numStudents);

    print(outFile, students, numStudents);

    inFile.close();
    outFile.close();


    return 0;
}

void read(ifstream& inFile, studentType students[], int numStudents)
{
    for (int i = 0; i < numStudents; i++)
    {
        inFile >> students[i].fName >> students[i].lName;
        for (int i2 = 0; i2 < 5; i2++)
            inFile >> students[i].scores[i2];
        inFile.ignore(100, '\n');
    }
}

void calcGrade(studentType students[], int numStudents)
{
    double averageScore;

    for (int i = 0; i < numStudents; i++)
    {
        int aggregateScore = 0;
        for (int i2 = 0; i2 < 5; i2++)
            aggregateScore += students[i].scores[i2];

        averageScore = aggregateScore / 5;
        if (averageScore >= 90)
            students[i].grade = 'A';
        else if (averageScore >= 80)
            students[i].grade = 'B';
        else if (averageScore >= 70)
            students[i].grade = 'C';
        else if (averageScore > 60)
            students[i].grade = 'D';
        else
            students[i].grade = 'F';
    }

}

void print(ofstream& outFile, studentType students[], int numStudents)
{
    outFile << right << "Name" << setw(40) << "Scores" << setw(30) << "Grade" << endl;
    for (int i = 0; i < numStudents; i++)
    {
        outFile << right << students[i].fName << " " << students[i].lName << setw(20);

        for (int i2 = 0; i2 < 5; i2++)
            outFile << right << " " << students[i].scores[i2];

        outFile << right << setw(20) << students[i].grade << endl;
    }




}

这就是我在输出文件中得到的:

Name                                  Scores                         Grade
ARCHIE ANDREW                    90 80 70 90 80                   B
BETTY COOPER                    100 100 100 100 100                   A
VERONICA LODGE                    70 70 70 80 80                   C
TARA DREW                    90 80 78 90 89                   B
NANCY COOPER                    100 100 10 90 100                   B
RONI DODGE                    70 77 70 88 80                   C
RICHIE DREW                    90 80 70 90 80                   B
BELLA HOOPER                    100 100 100 100 100                   A
NINA HODGE                    70 70 70 80 80                   C
CLARA DEW                    90 80 78 90 89                   B
FANCY COOP                    100 100 10 90 100                   B
RINA EDGE                    70 77 70 88 80                   C
4

0 回答 0