C++程序设计学籍管理有错误

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

// 学生类
class Student {
public:
    string id;  // 学号
    string name;  // 姓名
    string gender;  // 性别
    string major;  // 专业
    string status;  // 学籍状态
    string hometown;  // 籍贯
    Student() {}
    Student(string id, string name, string gender, string major, string status, string hometown) :
        id(id), name(name), gender(gender), major(major), status(status), hometown(hometown) {}
};

// 成绩类
class Grade {
public:
    string id;  // 学号
    string course;  // 课程名称
    double score;  // 成绩
    Grade() {}
    Grade(string id, string course, double score) : id(id), course(course), score(score) {}
};

// 学生管理系统类
class StudentManagementSystem {
private:
    vector<Student> students;  // 学生信息
    vector<Grade> grades;  // 成绩信息
    int max_id;  // 每输入一个学生信息学号顺序加1
public:
    StudentManagementSystem() : max_id(0) {}
    ~StudentManagementSystem() {}

    // 打印菜单
    void print_menu() {
        cout << "===========================" << endl;
        cout << "1.录入学生基本信息" << endl;
        cout << "2.显示/修改/增加/删除学生信息" << endl;
        cout << "3.学生基本情况查询" << endl;
        cout << "4.学生成绩查询" << endl;
        cout << "5.全部学生信息查询" << endl;
        cout << "6.统计各专业的成绩,性别比例及籍贯" << endl;
        cout << "7.保存学生信息和成绩信息" << endl;
        cout << "0.退出程序" << endl;
        cout << "===========================" << endl;
    }

    // 读取学生信息和成绩信息
    void load_data() {
        ifstream fin("students.txt");
        if (fin.good()) {
            string line;
            while (getline(fin, line)) {
                string id, name, gender, major, status, hometown;
                istringstream iss(line);
                iss >> id >> name >> gender >> major >> status >> hometown;
                students.push_back(Student(id, name, gender, major, status, hometown));
                max_id = max(max_id, stoi(id));
            }
        }
        fin.close();

        fin.open("grades.txt");
        if (fin.good()) {
            string line;
            while (getline(fin, line)) {
                string id, course;
                double score;
                istringstream iss(line);
                iss >> id >> course >> score;
                grades.push_back(Grade(id, course, score));
            }
        }
        fin.close();
    }

    // 保存学生信息和成绩信息
    void save_data() {
        ofstream fout("students.txt");
        for (const auto& s : students) {
            fout << s.id << " " << s.name << " " << s.gender << " " << s.major << " " << s.status << " " << s.hometown << endl;
        }
        fout.close();

        fout.open("grades.txt");
        for (const auto& g : grades) {
            fout << g.id << " " << g.course << " " << g.score << endl;
        }
        fout.close();
    }

    // 录入学生信息
    void add_student() {
        string name, gender, major, status, hometown;
        cout << "请输入学生姓名:";
        cin >> name;
        cout << "请输入学生性别:";
        cin >> gender;
        cout << "请输入学生专业:";
        cin >> major;
        cout << "请输入学生学籍状态:";
        cin >> status;
        cout << "请输入学生籍贯:";
        cin >> hometown;
        max_id++;
        students.push_back(Student(to_string(max_id), name, gender, major, status, hometown));
        cout << "录入成功!" << endl;
    }

    // 显示, 修改, 增加, 删除学生信息
    void manage_student() {
        cout << "请输入操作编号(1.显示全部学生信息;2.修改学生信息;3.增加学生信息;4.删除学生信息;0.返回):";
        int choice;
        cin >> choice;
        if (choice == 0) {
            return;
        }
        else if (choice == 1) {
            cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
            for (const auto& s : students) {
                cout << left << setw(4) << s.id << setw(10) << s.name << setw(6) << s.gender << setw(12) << s.major << setw(6) << s.status << setw(10) << s.hometown << endl;
            }
        }
        else if (choice == 2) {
            string id, name, gender, major, status, hometown;
            cout << "请输入要修改的学生学号:";
            cin >> id;
            auto it = find_if(students.begin(), students.end(), [&](const Student& s) { return s.id == id; });
            if (it != students.end()) {
                cout << "当前学生信息:" << endl;
                cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
                cout << left << setw(4) << it->id << setw(10) << it->name << setw(6) << it->gender << setw(12) << it->major << setw(6) << it->status << setw(10) << it->hometown << endl;
                cout << "请输入修改后的学生姓名:";
                cin >> name;
                cout << "请输入修改后的学生性别:";
                cin >> gender;
                cout << "请输入修改后的学生专业:";
                cin >> major;
                cout << "请输入修改后的学生学籍状态:";
                cin >> status;
                cout << "请输入修改后的学生籍贯:";
                cin >> hometown;
                *it = Student(id, name, gender, major, status, hometown);
                cout << "修改成功!" << endl;
            }
            else {
                cout << "未找到该学生!" << endl;
            }
        }
        else if (choice == 3) {
            add_student();
        }
        else if (choice == 4) {
            string id;
            cout << "请输入要删除的学生学号:";
            cin >> id;
            auto it = find_if(students.begin(), students.end(), [&](const Student& s) { return s.id == id; });
            if (it != students.end()) {
                students.erase(it);
                cout << "删除成功!" << endl;
            }
            else {
                cout << "未找到该学生!" << endl;
            }
        }
        else {
            cout << "无效操作!" << endl;
        }
    }

    // 学生基本情况查询
    void search_student() {
        cout << "请输入查询方式编号(1.查找学号;2.查找姓名;3.查找专业):";
        int choice;
        cin >> choice;
        string keyword;
        if (choice == 1) {
            cout << "请输入学号:";
            cin >> keyword;
            auto it = find_if(students.begin(), students.end(), [&](const Student& s) { return s.id == keyword; });
            if (it != students.end()) {
                cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
                cout << left << setw(4) << it->id << setw(10) << it->name << setw(6) << it->gender << setw(12) << it->major << setw(6) << it->status << setw(10) << it->hometown << endl;
            }
            else {
                cout << "未找到该学生!" << endl;
            }
        }
        else if (choice == 2) {
            cout << "请输入姓名:";
            cin >> keyword;
            auto it = find_if(students.begin(), students.end(), [&](const Student& s) { return s.name == keyword; });
            if (it != students.end()) {
                cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
                cout << left << setw(4) << it->id << setw(10) << it->name << setw(6) << it->gender << setw(12) << it->major << setw(6) << it->status << setw(10) << it->hometown << endl;
            }
            else {
                cout << "未找到该学生!" << endl;
            }
        }
        else if (choice == 3) {
            cout << "请输入专业:";
            cin >> keyword;
            cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
            for (const auto& s : students) {
                if (s.major == keyword) {
                    cout << left << setw(4) << s.id << setw(10) << s.name << setw(6) << s.gender << setw(12) << s.major << setw(6) << s.status << setw(10) << s.hometown << endl;
                }
            }
        }
        else {
            cout << "无效操作!" << endl;
        }
    }

    // 学生成绩查询
    void search_grade() {
        string id;
        cout << "请输入学生学号:";
        cin >> id;
        auto it = find_if(students.begin(), students.end(), [&](const Student& s) { return s.id == id; });
        if (it == students.end()) {
            cout << "未找到该学生!" << endl;
            return;
        }
        cout << "当前学生成绩信息:" << endl;
        cout << left << setw(10) << "课程名称" << setw(6) << "成绩" << endl;
        bool found = false;
        for (const auto& g : grades) {
            if (g.id == id) {
                cout << left << setw(10) << g.course << setw(6) << g.score << endl;
                found = true;
            }
        }
        if (!found) {
            cout << "无成绩信息!" << endl;
        }
    }

    // 全部学生信息查询
    void search_all_students() {
        cout << left << setw(4) << "学号" << setw(10) << "姓名" << setw(6) << "性别" << setw(12) << "专业" << setw(6) << "状态" << setw(10) << "籍贯" << endl;
        for (const auto& s : students) {
            cout << left << setw(4) << s.id << setw(10) << s.name << setw(6) << s.gender << setw(12) << s.major << setw(6) << s.status << setw(10) << s.hometown << endl;
        }
    }

    // 统计各专业的成绩,性别比例及籍贯
    void stat() {
        cout << left << setw(12) << "专业" << setw(10) << "平均成绩" << setw(10) << "女生比例" << setw(10) << "籍贯" << endl; 
        for (const auto& s : students) {
            auto it = find_if(grades.begin(), grades.end(), [&](const Grade& g) { return g.id == s.id; });
            if (it != grades.end()) {
                double sum = 0;
                int count = 0;
                for (const auto& g : grades) {
                    if (g.id == s.id) {
                        sum += g.score;
                        count++;
                    }
                }
                double avg_score = sum / count;

                int female_count = count_if(students.begin(), students.end(), [&](const Student& x) { return x.major == s.major && x.gender == "女"; });
                
                vector<string> hometowns;
                for (const auto& x : students) {
                    if (x.major == s.major) {
                        hometowns.push_back(x.hometown);
                    }
                }
                sort(hometowns.begin(), hometowns.end());
                auto it = unique(hometowns.begin(), hometowns.end());
                hometowns.erase(it, hometowns.end());

                cout << left << setw(12) << s.major << setw(10) << fixed << setprecision(2) << avg_score << setw(10) << fixed << setprecision(0) << (double)female_count / count << setw(10);
                for (int i = 0; i < (int)hometowns.size(); i++) {
                    if (i > 0) {
                        cout << ",";
                    }
                    cout << hometowns[i];
                }
                cout << endl;
            }
        }
    }
};

int main() {
    StudentManagementSystem system;
    system.load_data();
    while (true) {
        system.print_menu();
        cout << "请输入操作编号:";
        int choice;
        cin >> choice;
        switch (choice) {
        case 0:
            system.save_data();
            cout << "程序已退出!" << endl;
            return 0;
        case 1:
            system.add_student();
            break;
        case 2:
            system.manage_student();
            break;
        case 3:
            system.search_student();
            break;
        case 4:
            system.search_grade();
            break;
        case 5:
            system.search_all_students();
            break;
        case 6:
            system.stat();
            break;
        case 7:
            system.save_data();
            cout << "已保存学生信息和成绩信息!" << endl;
            break;
        default:
            cout << "无效操作!" << endl;
            break;
        }
    }
}

img


为什么还是有错误,打了好久了。

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7700388
  • 这篇博客也不错, 你可以看下C++简单学生管理程序【实现输出成绩,查找学号等】
  • 除此之外, 这篇博客: C++程序设计基础知识学习中的 算数运算符于表达式: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 基本算术运算符:

    1. float型数据运算时会转化为double型
    2. int型运算时有时采用“向零取整”,有时采用“向下取整”
    int i = -5/3;
    
    //向零取整
    i = -1;
    
    //向下取整
    i = -2;
    
    

    运算符之间的优先等级:(优先级越低优先度越高,仅列出部分运算符)

    优先级运算符结合方向
    2() [] -> . ++(后置时) --(后置时)自左向右
    3++(前置时) --(前置时) ! -(做符号时) +(做符号时) *(指针) &(取址) (类型)(类型转换) sizeof new delete自右向左
    4* / %自左向右
    5+ -自左向右
    6<<(左位移运算) >>(右位移运算)自左向右
    7< <= >= >自左向右
    8== !=自左向右

    混合数据类型运算时数据类型的处理:有符号转无符号,short转long,char转int,int转double,float转double(float参与运算时自动转double)
    例:

    int i;
    char c;
    float f;
    double d;
    
    //省略赋值细节
    ...
    
    10 + 'a' + i * f - d
    //先计算10 + 'a' 此时将char型字符'a'转为int型整数
    //再计算i * f,由于int型和float型混合运算,float型运算时转为double型,因此结果为double型
    //综上为:int + char + int * float(double) - double = double
    
    //下列两式之间等价
    x % = y + 8;
    x = x % (y + 8);
    
    //下式最后a的值为60
    a = 3 * 5 , a * 4
    
  • 您还可以看一下 王健伟老师的C++语言基础到进阶课程中的 模板概念,函数模板定义、调用小节, 巩固相关知识点