qq_52239400 2021-07-06 23:01 采纳率: 100%
浏览 23
已采纳

c++运行出错内存问题

img
上面是运行遇到的错误,编译是通过的
原本186行187行是没有new的 遇到的问题是system.nullreferenceexception
不知道要如何修改了 望大佬相助
做一个人员信息管理系统
以下是源代码

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
void check(double x)
{if(x<0)throw x;}
class people
{public:
 void print()
 {cout<<"名字"<<number<<"生日"<<birthday<<"年龄"<<age<<"名字"<<name<<"性别"<<sex<<"科目"<<major<<"系别"<<department;};
    int age,i,j;
    string number,birthday,name,sex,major,department;
};
class student:virtual public people
{public:
    friend istream& operator>>(istream &in,student &p);
    friend ostream& operator<<(ostream &out,student &p);
    void adddata();
    void deletedata();
    void updatedata();
    void search();
    void findmaxdata();
    void displaydata();
protected:
    int chinese,math,english;
};
student stu[30];
istream& operator>>(istream &in,student &p)
    {in>>p.number>>p.birthday>>p.age>>p.name>>p.sex>>p.major>>p.department>>p.chinese>>p.math>>p.english;
     return in;};
    ostream& operator<<(ostream &out,student &p)
    {out<<"名字"<<p.number<<"生日"<<p.birthday<<"年龄"<<p.age<<"名字"<<p.name<<"性别"<<p.sex<<"科目"<<p.major<<"系别"<<p.department<<"语文"<<p.chinese<<"数学"<<p.math<<"英语"<<p.english;
     return out;};
void student::adddata()
{cout<<"请分别输入学号 生日 年龄 名字 性别 专业 系别 语文成绩 数学成绩 英语成绩";
 cin>>stu[i];
 i++;
};

void student::deletedata()
{string del;
 int v=0;
 cin>>del;
 for(v=0;v<i;v++)
 if(stu[v].number==del)
break;
for(v;v<i;v++)
stu[v]=stu[v+1];
i--;
};

void student::updatedata()
{string del;
 cin>>del;
 for(int v=0;v<i;v++)
 if(stu[v].number==del)
    break;
 cout<<"请分别输入学号 生日 年龄 名字 性别 专业 系别 语文成绩 数学成绩 英语成绩";
 cin>>number>>birthday>>age>>name>>sex>>major>>department>>chinese>>math>>english;
};

void student::search()
{string fi;
 cin>>fi;
 for(int e=0;e<i;e++)
 if(stu[e].name==fi||stu[e].number==fi||stu[e].sex==fi)
     cout<<stu[e]<<endl;
};

void student::findmaxdata()
{int max=0;
 for(int m=0;m<i;m++)
 if(stu[m].english>max)
 max=stu[m].english;
 cout<<max<<endl;
};

void student::displaydata()
{for(int m=0;m<i;m++)
cout<<stu[m]<<endl;
};

class teacher:virtual public people
{public:
    friend istream& operator>>(istream &in,teacher &p);
    friend ostream& operator<<(ostream &out,teacher &p);
    void adddata();
    void deletedata();
    void updatedata();
    void search();
    void findmaxdata();
    void displaydata();
protected:
    string title;double salary;
};
teacher tea[30];
istream& operator>>(istream &in,teacher &p)
{in>>p.number>>p.birthday>>p.age>>p.name>>p.sex>>p.major>>p.department>>p.title>>p.salary;     
return in;
};
ostream& operator<<(ostream &out,teacher &p)
{cout<<"名字"<<p.number<<"生日"<<p.birthday<<"年龄"<<p.age<<"名字"<<p.name<<"性别"<<p.sex<<"科目"<<p.major<<"系别"<<p.department<<"职称"<<p.title<<"工资"<<p.salary;
return out;
};
void teacher::adddata()
{cout<<"请分别输入学号 生日 年龄 名字 性别 专业 系别 职位 工资";
 cin>>tea[j];
 j++;
try
{check(salary);}
catch(...)
{cout<<"工资异常"<<endl;}
};

void teacher::deletedata()
{string del;
int v=0;
 cin>>del;
 for(v=0;v<j;v++)
 if(tea[v].number==del)
     break;
 for(v;v<j;v++)
 tea[v]=tea[v+1];
 j--;
};

void teacher::updatedata()
{string del;
 cin>>del;
 for(int v=0;v<j;v++)
 if(tea[v].number==del)
     break;
 cout<<"请分别输入学号 生日 年龄 名字 性别 专业 系别 职位 工资";
 cin>>number>>birthday>>age>>name>>sex>>major>>department>>title>>salary;
 try
 {check(salary);}
 catch(...)
 {cout<<"工资异常"<<endl;}
};
 
void teacher::search()
{string fi;
 cin>>fi;
 for(int q=0;q<j;q++)
 if(tea[q].name==fi||tea[q].number==fi||tea[q].sex==fi)
 cout<<tea[q]<<endl;
};

void teacher::findmaxdata()
{double maxs=0;
 for(int n=0;n<j;n++)
 if(tea[n].salary>maxs)
 maxs=tea[n].salary;
 cout<<maxs<<endl;
};

void teacher::displaydata()
{for(int e=0;e<j;e++)
cout<<tea[e]<<endl;
};

int main()
{int choice,time=0,choice2,i=0,j=0;
 string test,password="12345";
 cout<<"请输入密码,您有三次机会(密码12345)"<<endl;
 while(time<3)
 {cin>>test;
 if(test==password)
    {time=10;system("cls");
    system("color 30");
    cout << "******************************************" << endl;
    cout << "*********  欢迎进入学生成绩管理系统  *********" << endl;
    cout << "              1.增加一位人员记录              " << endl;
    cout << "              2.删除一个人员记录              " << endl;
    cout << "              3.修改一个人员记录              " << endl;
    cout << "              4.查找记录                      " << endl;
    cout << "              5.查找英语最高或工资最高成员    " << endl;
    cout << "              6.显示全部人员记录              " << endl;
    cout << "              7.从外部文件追加数据            " << endl;
    cout << "              8.将所有数据保存到磁盘文件      " << endl;
    cout << "              9.退出系统                      " << endl;
    cout << "******************************************" << endl;
    cout << "            选择1-9数字进行操作                         "<<endl;
    cout << endl;
 student *p=new student();
 teacher *p1=new teacher();
 cin>>choice;
 switch(choice)
 {
 case 1:
     {
      cout<<"学生输入1,教师输入2"<<endl;
      cin>>choice2;
      if(choice2==1)
          {p->adddata();}
      else
      {p1->adddata();}
     }
 case 2:
      {cout<<"学生输入1,教师输入2"<<endl;
      cin>>choice2;
      if(choice2==1)
          {p->deletedata();}
      else
          {p1->deletedata();}
      }
 case 3:
     {cout<<"学生输入1,教师输入2"<<endl;
      cin>>choice2;
      if(choice2==1)
          {p->updatedata();}
      else
      {p1->updatedata();}
     }
 case 4:
     {cout<<"学生输入1,教师输入2"<<endl;
      cin>>choice2;
      if(choice2==1)
          {p->search();}
      else
      {p1->search();}
     }
 case 5:{cout<<"学生输入1,教师输入2"<<endl;
      cin>>choice2;
      if(choice2==1)
          {p->findmaxdata();}
      else
      {p1->findmaxdata();}
        }
 case 6:
     {    p->displaydata();
        p1->displaydata();}
 default:
     {break;}
 }
     }
 else 
 {time++;
 cout<<"您还有"<<3-time<<"次机会"<<endl;}
 }
 return 0;
}
  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-07-07 08:42
    关注

    主要是你这个代码比较奇葩了一些,明明所有教师学生信息都在stu[30]和tea[30]中,但你却new了两个不知道干什么用的p1和p,然后还用它来adddata之类的,可这些函数都在操作tea和stu数组,然后又用cin输入数据,输入的数据又是写到p1和p这个对象的,和stu,tea没一毛钱关系啊。
    最有问题的,就是这个基类、子类了。基类有啥用呢?什么也没有,子类有啥用的,多态性一点也没用到,全部if...else...,那你建立父类子类干啥呢?毫无关系的两个类也一样用啊

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题