You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++结构体数组问题:成绩输出文件仅显示最后一名学生数据如何修复

问题根因

你遇到的文件仅保留最后1条学生数据的问题,和数组无关,是文件打开逻辑错误导致的:

  • ofstream默认的ios::out打开模式会清空文件原有内容,你每处理一个学生就重新打开一次文件,之前写入的内容会被直接清空,自然只剩下最后一次写入的内容。
  • 现有逻辑每次写入学生信息都会重复生成表头,也会导致文件内容格式混乱。
修复方案

推荐对逻辑做以下调整:

  1. 把文件打开操作从print函数移到main函数中,只打开一次文件,提前写入表头,避免反复开闭文件清空内容
  2. 修改print函数,接收已打开的文件流引用作为参数,直接写入学生数据
  3. 所有学生数据处理完成后再统一关闭文件

修改后可运行的完整代码:

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

using namespace std;

struct students
{
    string name;
    int ID{};
    double exam1{}, exam2{}, final{}, lab{}, homework{}, avg{};
};

void getDetails(students& student)
{
    cout << "Enter student name: ";
    cin >> student.name;

    cout << "Enter student ID: ";
    cin >> student.ID;

    cout << "Enter their grade for the first exam: ";
    cin >> student.exam1;

    cout << "Enter their grade for the second exam: ";
    cin >> student.exam2;

    cout << "Enter their grade for the final exam: ";
    cin >> student.final;

    cout << "Enter their lab grade: ";
    cin >> student.lab;

    cout << "Enter their homework grade: ";
    cin >> student.homework;

    cout << "\n";
}

double calculateFinalGrade(students& student)
{
    double avg;
    avg = ((student.exam1 * .15) + (student.exam2 * .15) + (student.final * .20) + (student.lab * .40) + (student.homework * .10));
    return avg;
}

double giveBonus(students& student)
{
    student.avg += 3;
    if (student.avg > 100)
    {
        student.avg = 100;
    }
    return student.avg;
}

// 修改print函数,接收文件流引用作为参数
void print(students& student, ofstream& outFile)
{
    outFile << student.name << setw(19) << student.ID << setw(16) << student.exam1 << setw(14) << student.exam2 << setw(16) << student.final << setw(17)
            << student.lab << setw(18) << student.homework << setw(17) << student.avg << endl;
}

int main()
{
    students studentArray[5];
    
    // 提前打开两个文件,写入表头
    ofstream outPass("pass.txt", ios::out);
    ofstream outFail("fail.txt", ios::out);
    outPass << fixed << showpoint << setprecision(2);
    outFail << fixed << showpoint << setprecision(2);
    // 只写一次表头
    string header = "Student Name" + string(15, ' ') + "Student ID" + string(13, ' ') + "Exam 1" + string(14, ' ') + "Exam 2" + string(18, ' ') + "Final Exam" + string(16, ' ') + "Lab Grade" + string(20, ' ') + "Homework Grade" + string(20, ' ') + "Adjusted Average\n";
    outPass << header;
    outFail << header;

    for (int i = 0; i < 5; i++)
    {
        cout << "Enter details for student " << (i + 1) << endl;
        getDetails(studentArray[i]);
        studentArray[i].avg = calculateFinalGrade(studentArray[i]);
        studentArray[i].avg = giveBonus(studentArray[i]);
    }

    for (int i = 0; i < 5; i++)
    {
        if (studentArray[i].avg >= 60) {
            print(studentArray[i], outPass);
        } else {
            print(studentArray[i], outFail);
        }
    }

    // 统一关闭文件
    outPass.close();
    outFail.close();

    return 0;
}

注意事项

运行代码前先删除本地旧的pass.txt和fail.txt,避免旧内容干扰测试结果。

内容的提问来源于stack exchange,提问作者Kristen N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 13:27:03