hkk522793060 2022-05-13 11:51 采纳率: 100%
浏览 591
已结题

遇到std::bad_alloc异常,现在还不理解到底是为什么出错,要怎么处理

遇到std::bad_alloc异常,现在还不理解到底是为什么出错,要怎么处理,请高人指点了


Classone.txt文件内容
张一 20 165 50 1 85 87 90 69 96 87 90 88 64
张二 21 160 37 2 56 87 94 60 78 80 64 77 66
张三 22 159 55 3 86 69 77 66 83 65 75 45 80
张四 20 158 53 4 75 66 90 56 63 78 66 80 93
张五 22 175 60 5 90 85 80 96 74 68 67 87 91
张六 22 177 50 6 100 60 68 87 93 94 63 76 94
张七 21 185 95 7 65 71 84 70 68 77 82 65 69
张八 20 174 80 9 82 76 69 78 88 92 68 79 77
张九 20 177 58 9 85 87 90 86 95 86 90 89 93
张十 20 164 82 10 87 71 73 78 84 90 65 68 92


实现功能:
在void CTeacher::ReadInfo()函数中实现
从Classone.txt文件中读取学生信息,分别赋值CStudent classone[10]中的各个量。分别计算各个学生的平均成绩,并对学生的成绩进行排序(平均成绩高的为第一名),将排名的结果写入名为Sort.txt的文件(写入的内容必须包括学生的各个信息)。


应该是在冒泡排序那几句遇到std::bad_alloc异常,但找不到解决方法



#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class CPerson
{
protected:
    string name;
    int age;
    int height;
    int weight;
public:
    CPerson();
    void ShowInfo();
    void Measure();
};
CPerson::CPerson() {
    height = 180;
    weight = 90;
    age = 40;
    name = "李四";
}
void CPerson::ShowInfo() {
    cout << "name:" << name;
    cout << "age:" << age;
    cout << "height:" << height;
    cout << "weight:" << weight << endl;
}
void CPerson::Measure() {
    float sta;
    sta = height - 105;
    if (weight < sta * 0.6)
        cout << "严重营养不良" << endl;
    else if (weight >= sta * 0.6 && weight < sta * 0.8)
        cout << "中度营养不良" << endl;
    else if (weight >= sta * 0.8 && weight < sta * 0.9)
        cout << "轻度营养不良" << endl;
    else if (weight >= sta * 0.9 && weight < sta * 1.1)
        cout << "正常范围" << endl;
    else if (weight > sta * 1.1)
        cout << "肥胖" << endl;
}
//class CStudent;
class CTeacher;
class CStudent :public CPerson {
protected:
    int num;
    int math;
    int chi;
    int geogra;
    int biolo;
    int chemis;
    int histo;
    int politi;
    int physi;
    float aver;
public:    
    friend CTeacher;
    void ShowInfo();
    CStudent();
    float operator()();
    //friend void CTeacher::SetScore(CStudent& p);
    //friend void CTeacher::ReadInfo();
};
class CTeacher :public CPerson {
protected:
    CStudent classone[10];
public:
    CTeacher();
    //void SetScore(CStudent& p);
    void ReadInfo();

};


//void CTeacher::SetScore(CStudent& p) {
//    cin>>p.math>>p.chi>>p.geogra>>p.biolo>>p.chemis>>p.histo>>p.politi>>p.physi;
//}

CTeacher::CTeacher(): classone() {
    height = 175;
    age = 52;
    name = "张老师";
    weight = 65;
    
}

CStudent::CStudent() {
    height = 0;
    weight = 0;
    age = 0;
    name = "0";
    num = 0;
    math = 0;
    chi = 0;
    geogra = 0;
    biolo = 0;
    chemis = 0;
    histo = 0;
    politi = 0;
    physi = 0;
}
void CStudent::ShowInfo() {
    CPerson::ShowInfo();
    cout << "num:" << num;
    cout << "math_score:" << math;
    cout << "chinese_score:" << chi;
    cout << "geogtaphy_score:" << geogra;
    cout << "biology_score:" << biolo;
    cout << "chemistry_score:" << chemis;
    cout << "history_score:" << histo;
    cout << "politics_score:" << politi;
    cout << "physics_score" << physi;
}
float CStudent::operator()() {
    float aver;
    aver = (math + chi + geogra + biolo + chemis + histo + politi + physi) / 8.0;
    return aver;
}
void CTeacher::ReadInfo()
{
    fstream file;
    file.open("Classone.txt", ios::in | ios::binary);
    if (!file.is_open()) {
        cerr << "error open" << endl;
        exit(1);
    }
    for (int i = 0; i < 10; i++)
        file.read((char*)&classone[i], sizeof(classone[i]));
    file.close();
    for (int i = 0; i < 10; i++)
        ((classone + i))->aver = (*(classone + i))();
    int j = 0;
    CStudent* t = new CStudent;
    for (int i = 0; i < 9; i++)
        for (j = 0; j < 9 - i; j++)
            if (classone[j].aver <  classone[j+1].aver) {
                *t = classone[j];
                classone[j] = classone[j+1];
                classone[j+1] = *t;
            }
    fstream file2;
    file2.open("Sort.txt", ios::out);
    for (int i = 0; i < 10; i++) {
        ((classone + i))->ShowInfo();
        file << "num:" << (classone + i)->num;
        file << "math_score:" << (classone + i)->math;
        file << "chinese_score:" << (classone + i)->chi;
        file << "geogtaphy_score:" << (classone + i)->geogra;
        file << "biology_score:" << (classone + i)->biolo;
        file << "chemistry_score:" << (classone + i)->chemis;
        file << "history_score:" << (classone + i)->histo;
        file << "politics_score:" << (classone + i)->politi;
        file << "physics_score" << (classone + i)->physi;
    }
    file2.close();

}



int main() {
    //CPerson LiSi;
    //LiSi.Measure();
    //CStudent Xiaoming;
    CTeacher zhang;
    //zhang.ShowInfo();
    //zhang.Measure();
    //zhang.SetScore(Xiaoming);
    //cout <<Xiaoming()<< endl;
    zhang.ReadInfo();
    return 0;
}
  • 写回答

3条回答 默认 最新

  • 关注

    file.read((char*)&classone[i], sizeof(classone[i])) -> 二进制方法读普通文本,没崩溃么。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月22日
  • 已采纳回答 5月14日
  • 赞助了问题酬金10元 5月13日
  • 创建了问题 5月13日

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办