「已注销」 2022-12-27 11:29 采纳率: 91.2%
浏览 30
已结题

学生管理系统,在“删除函数”中出现了如图所示的错误,其他函数已经测试过,都是对的,请问这个错误要怎么修改?

img

错误在下面两张图片中,是在测试“删除函数”时出现了如上图所示的错误

img


img

以下是所有完整代码
这是“studentManager.h”的代码


#pragma once
#include <iostream>
using namespace std;
#include"student.h"
#include"exstudent.h"
#include"comstudent.h"

#include<fstream>
#define FILENAME "学生信息.txt"
class StudentManager
{
public:

    //构造函数
    StudentManager();

    //展示菜单
    void Show_Menu();

    //退出函数
    void ExitSystem();

    //记录学生人数
    int stuNum;

    //学生数组指针
    Student** stuArray;

    //添加学生
    void add();

    //保存文件
    void save();

    //判断文件是否为空的标志
    bool m_FileIsEmpty;

    //统计文件中的人数
    int get_stuNum();

    void init_Stu();

    void Show_Stu();

    void Del_Stu();

    //判断学生是否存在 如果存在返回职工所在数组中的位置,不存在返回-1
    int IsExist(string Num);
    //析构函数
    ~StudentManager();
};

这是“student.h”的代码

#pragma once
#include<iostream>
using namespace std;
#include<string>
class Student
{
public:
    string stu_name;//姓名
    string stu_sex;    //性别
    string stu_num;    //学号
    int stu_grade;    //年级
    string stu_college;    //学院
    string stu_system;    //系
    int stu_class;    //班级
    string stu_dormitory;    //宿舍
    string stu_home;        //籍贯
    string stu_phone;        //联系电话

    //成绩
    int stu_English;
    int stu_Maths;
    int stu_Programme;

    int dId;//学生类别

    //显示个人信息
    virtual void showInfo() = 0;//在抽象类中不作任何的实现
};

这是“exstudent.h”的代码

#pragma once
//普通学生
#include<iostream>
using namespace std;
#include"student.h"
class ExStudent :public Student
{
public:
    ExStudent(string name, string sex, string num, int grade, string college, string system, int st_class, string dormitory, string home, string phone, int English, int Maths, int Programme,int dId);
    //显示个人信息
    virtual void showInfo();

    virtual string getDeptName();
};


这是“comstudent.h”的代码


#pragma once
//普通学生
#include<iostream>
using namespace std;
#include"student.h"
class ComStudent :public Student
{
public:
    ComStudent(string name, string sex, string num, int grade, string college, string system, int st_class, string dormitory, string home, string phone, int English, int Maths, int Programme,int dId);
    //显示个人信息
    virtual void showInfo();
    
    virtual string getDeptName();
};

这是“studentManager.cpp”的代码

#include"studentManager.h"

StudentManager::StudentManager()
{
    //1、文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);    //读文件
    if (!ifs.is_open())
    {
      
    //初始化属性,初始化记录人数
        this->stuNum = 0;
        //初始化数组指针
        this->stuArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }

    //2、文件存在,数据为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        
        //初始化记录人数
        this->stuNum = 0;
        //初始化数组指针
        this->stuArray= NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    // 3、文件存在,并且记录数据
        int num = this->get_stuNum();
     cout << "学生人数为:" << num << endl;
    this->stuNum = num;

    ////开辟空间
    this->stuArray = new Student * [this->stuNum];

    ////将文件中的数据,存到数组中
    this->init_Stu();

}

//统计文件中的人数
int StudentManager::get_stuNum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);    //打开文件 读

    string name;//姓名
    string sex;    //性别
    string num;    //学号
    int grade;    //年级
    string college;    //学院
    string system;    //系
    int st_class;    //班级
    string dormitory;    //宿舍
    string home;        //籍贯
    string phone;        //联系电话

    //成绩
    int English;
    int Maths;
    int Programme;

    int Id;

    int n = 0;

    while (ifs >> name && ifs >> sex && ifs >> num && ifs >> grade && ifs >> college && ifs >> system && ifs >> st_class && ifs >> dormitory && ifs >> home && ifs >> phone && ifs >> English && ifs >> Maths && ifs >> Programme&&ifs>>Id)
    {
        //统计人数变量
        n++;
    }
    return n;
}

//展示菜单
void StudentManager::Show_Menu()
{
    cout << "-------------------------------------------------------" << endl;
    cout << "---------------->>学生信息管理系统<<-----------------" << endl;
    cout << "       本系统中编号为1是普通学生,编号为2是交换生    " << endl;
    cout << "--------------------->>主菜单<<----------------------" << endl;
    cout << "-----------------------------------------------------" << endl;
    cout << "*                0.退出学生管理系统                  *" << endl;
    cout << "*                1.增加学生信息                      *" << endl;
    cout << "*                2.显示学生信息                      *" << endl;
    cout << "*                3.删除学生信息                      *" << endl;
    cout << "*                4.修改学生信息                      *" << endl;
    cout << "*                5.查找学生信息                      *" << endl;
    cout << "*                6.排序学生信息                      *" << endl;
    cout << endl;
}

void StudentManager::ExitSystem()
{
    cout << "欢迎下次再来!" << endl;
    system("pause");
    exit(0);
}

//添加学生
void  StudentManager::add()
{
    cout << "请输入添加学生的数量:" << endl;
    int addNum = 0;
    cin >> addNum;
    if (addNum > 0)
    {
        //添加
        //计算添加新空间的大小
        int newSize = this->stuNum+ addNum;//新空间的人数=原来记录的人数+新增人数

        //开辟新空间
        Student** newSpace = new Student * [newSize];

        //将原来空间下的数据,拷贝到新空间下
        if (this->stuArray != NULL)
        {
            for (int i = 0; i < this->stuNum; i++)
            {
                newSpace[i] = this->stuArray[i];
            }
        }
        //添加新数据
        for (int i = 0; i < addNum; i++)
        {
            string name;//姓名
            string sex;    //性别
            string num;    //学号
            int grade;    //年级
            string college;    //学院
            string system;    //系
            int st_class;    //班级
            string dormitory;    //宿舍
            string home;        //籍贯
            string phone;        //联系电话

            //成绩
            int English;
            int Maths;
            int Programme;
            int Id;

            int dSelect;     //学生类别的选择

            cout << "请输入第" << i + 1 << "个学生的姓名:" << endl;
            cin >> name;
            cout << "请输入第" << i + 1 << "个学生的性别:" << endl;
            cin >> sex;
            cout << "请输入第" << i + 1 << "个学生的学号:" << endl;
            cin >> num;
            cout << "请输入第" << i + 1 << "个学生的年级:" << endl;
            cin >> grade;
            cout << "请输入第" << i + 1 << "个学生的学院:" << endl;
            cin >> college;
            cout << "请输入第" << i + 1 << "个学生的系:" << endl;
            cin >> system;
            cout << "请输入第" << i + 1 << "个学生的班级:" << endl;
            cin >> st_class;
            cout << "请输入第" << i + 1 << "个学生的宿舍:" << endl;
            cin >> dormitory;
            cout << "请输入第" << i + 1 << "个学生的籍贯:" << endl;
            cin >> home;
            cout << "请输入第" << i + 1 << "个学生的联系电话:" << endl;
            cin >> phone;
            cout << "请输入第" << i + 1 << "个学生的英语成绩:" << endl;
            cin >> English;
            cout << "请输入第" << i + 1 << "个学生的数学成绩:" << endl;
            cin >> Maths;
            cout << "请输入第" << i + 1 << "个学生的编程成绩:" << endl;
            cin >> Programme;
            cout << "请输入第" << i + 1 << "个学生的类别,1为普通学生,2为交换生:" << endl;
            cin >> Id;

            cout << "请选择该学生的类别:" << endl;
            cout << "1、普通学生" << endl;
            cout << "2、交换生" << endl;
            cin >> dSelect;

            Student* student = NULL;
            switch (dSelect)
            {
            case 1:
                student = new ComStudent(name, sex, num, grade, college, system, st_class, dormitory, home, phone, English, Maths, Programme,Id);
                break;
            case 2:
                student = new ExStudent(name, sex, num, grade, college, system, st_class, dormitory, home, phone, English, Maths, Programme,Id);
                break;
            default:
                break;
            }
            //将创建的职工职责,保存到数组中
            newSpace[this->stuNum + i] = student;

        }
        //释放原有的空间
        delete[]this->stuArray;

        //更改新空间的指向
        this->stuArray = newSpace;

        //更新新的职工人数
        this->stuNum = newSize;

        ////更新职工不为空的标志
        //this->m_FileIsEmpty = false;

        //提示添加成功
        cout << "成功添加" << addNum << "名新学生!" << endl;

        this->save();//保存数据到文件中
    }
    else
    {
        cout << "输入数据有误!" << endl;
    }
    //按任意键后,清屏回到上级目录
    system("pause");
    system("cls");

}

void StudentManager::save()
{

    ofstream ofs;
    ofs.open(FILENAME, ios::out);//用输出的方式打开文件——写文件
    for (int i = 0; i < this->stuNum; i++)
    {
        ofs << this->stuArray[i]->stu_name << " "
            << this->stuArray[i]->stu_sex << " "
            << this->stuArray[i]->stu_num << " "
            << this->stuArray[i]->stu_grade << " "
            << this->stuArray[i]->stu_college << " "
            << this->stuArray[i]->stu_system << " "
            << this->stuArray[i]->stu_class << " "
            << this->stuArray[i]->stu_dormitory << " "
            << this->stuArray[i]->stu_home << " "
            << this->stuArray[i]->stu_phone << " "
            << this->stuArray[i]->stu_English << " "
            << this->stuArray[i]->stu_Maths << " "
            << this->stuArray[i]->stu_Programme<<" "
            << this->stuArray[i]->dId << endl;
    }
    ofs.close();

}


//初始化学生;
void StudentManager::init_Stu()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    string name;//姓名
    string sex;    //性别
    string num;    //学号
    int grade;    //年级
    string college;    //学院
    string system;    //系
    int st_class;    //班级
    string dormitory;    //宿舍
    string home;        //籍贯
    string phone;        //联系电话

    //成绩
    int English;
    int Maths;
    int Programme;
    int Id;

    int index = 0;
    while (ifs >> name && ifs >> sex && ifs >> num && ifs >> grade && ifs >> college && ifs >> system && ifs >> st_class && ifs >> dormitory && ifs >> home && ifs >> phone && ifs >> English && ifs >> Maths && ifs >> Programme&&ifs>>Id)
    {
        Student* student = NULL;
        if (Id == 1)
        {
            student=new ComStudent(name, sex, num, grade, college, system, st_class, dormitory, home, phone, English, Maths, Programme, Id);
        }
        else
        {
            student = new ExStudent(name, sex, num, grade, college, system, st_class, dormitory, home, phone, English, Maths, Programme, Id);
        }
       
        this->stuArray[index] = student;
        index++;
    }
    ifs.close();
}

//显示学生信息
void StudentManager::Show_Stu()
{
    //判断文件是否为空
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空!" << endl;

    }
    else
    {
        for (int i = 0; i < stuNum; i++)
        {
            //利用多态调用程序接口
            this->stuArray[i]->showInfo();
        }
    }
    system("pause");
    system("cls");
}

void StudentManager::Del_Stu()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空!" << endl;

    }
    else
    {
        //按照学号删除
        cout << "请输入想要删除的学号:" << endl;
        string Num = 0;
        cin >> Num;

        int index = this->IsExist(Num);

        if (index != -1)//说明学生存在,并且要删除index位置上的学生
        {

            for (int i = index; i < this->stuNum; i++)
            {
                //数据前移
                this->stuArray[i] = this->stuArray[i + 1];
            }
            this->stuNum--;//更新数组中记录的人员个数

            //数据同步更新到文件中
            this->save();
            cout << "删除成功!" << endl;

        }
        else
        {
            cout << "删除失败,未找到该员工" << endl;
        }
    }
    system("pause");
    system("cls");
}

//判断学生是否存在 如果存在返回职工所在数组中的位置,不存在返回-1
int  StudentManager::IsExist(string Num)
{
    int index = -1;
    for (int i = 0; i < this->stuNum; i++)
    { 
        if (this->stuArray[i]->stu_num == Num)
        {
            
            //找到学生
            index = i;
            break;
        }
    }
    return index;
}

StudentManager::~StudentManager()
{

}

这是“comstudent.cpp”的代码

#include"comstudent.h"

ComStudent::ComStudent(string name, string sex, string num, int grade, string college, string system, int st_class, string dormitory, string home, string phone, int English, int Maths, int Programme,int Id)
{
    this->stu_name = name;
    this->stu_sex = sex;
    this->stu_num = num;
    this->stu_grade = grade;
    this->stu_college = college;
    this->stu_system = system;
    this->stu_class = st_class;
    this->stu_dormitory = dormitory;
    this->stu_home = home;
    this->stu_phone = phone;
    this->stu_English = English;
    this->stu_Maths = Maths;
    this->stu_Programme = Programme;
    this->dId = Id;
}

//显示个人信息
void ComStudent::showInfo()
{
    cout << "姓名:" << this->stu_name
        << "\t性别:" << this->stu_sex
        << "\t学号:" << this->stu_num
        << "\t年级:" << this->stu_grade
        << "\t学院:" << this->stu_college
        << "\t系:" << this->stu_system
        << "\t班级:" << this->stu_class<<endl
        << "宿舍:" << this->stu_dormitory
        << "\t籍贯:" << this->stu_home
        << "\t电话:" << this->stu_phone
        << "\t英语成绩:" << this->stu_English
        << "\t数学成绩:" << this->stu_Maths
        << "\t编程成绩:" << this->stu_Programme
        << "\t学生类别:" << this->dId << endl;
}

//学生类型
string ComStudent::getDeptName()
{
    return string("普通学生");
}


这是“exstudent.cpp”的代码

#include"exstudent.h"

ExStudent::ExStudent(string name, string sex, string num, int grade, string college, string system, int st_class, string dormitory, string home, string phone, int English, int Maths, int Programme,int Id)
{
    this->stu_name = name;
    this->stu_sex = sex;
    this->stu_num = num;
    this->stu_grade = grade;
    this->stu_college = college;
    this->stu_system = system;
    this->stu_class = st_class;
    this->stu_dormitory = dormitory;
    this->stu_home = home;
    this->stu_phone = phone;
    this->stu_English = English;
    this->stu_Maths = Maths;
    this->stu_Programme = Programme;
    this->dId = Id;
}

//显示个人信息
void ExStudent::showInfo()
{
    cout << "姓名:" << this->stu_name
        << "\t性别:" << this->stu_sex
        << "\t学号:" << this->stu_num
        << "\t年级:" << this->stu_grade
        << "\t学院:" << this->stu_college
        << "\t系:" << this->stu_system
        << "\t班级:" << this->stu_class << endl
        << "宿舍:" << this->stu_dormitory
        << "\t籍贯:" << this->stu_home
        << "\t电话:" << this->stu_phone
        << "\t英语成绩:" << this->stu_English
        << "\t数学成绩:" << this->stu_Maths
        << "\t编程成绩:" << this->stu_Programme
        << "\t学生类别:" << this->dId << endl;
}

//学生类型
string ExStudent::getDeptName()
{
    return string("交换生");
}


这是“学生管理系统1.cpp”的代码

#include<iostream>
using namespace std;
#include"studentManager.h"
#include"student.h"
#include"comstudent.h"
#include"exstudent.h"
int main()
{
    
////测试代码
//    Student* worker = NULL;
//    worker = new ComStudent("张三","男","202183290001" ,2021,"计算机学院","计算机系" ,  5 ,"沁35栋211","江苏" ," 13770787666", 59,  99 , 99);
//    worker->showInfo();
//    delete worker;
//
//    worker = new ComStudent("李四", "男", "202183290002", 2021, "计算机学院", "计算机系", 5, "沁35栋201", "江苏", " 13770087666", 59, 89, 59);
//    worker->showInfo();
//    delete worker;

    
    
    
    StudentManager wm;
    int choice = 0;
    while (true)
    {    
        
        wm.Show_Menu();
        cout << "请输入您的选择:" << endl;
        cin >> choice;
        switch (choice)
        {
        case 0:
            wm.ExitSystem();     //退出 
            break;
        case 1:                    //添加
            wm.add();
            break;    //增加学生信息
        case 2:        //显示
            wm.Show_Stu();
            break;
        case 3://删除
            wm.Del_Stu();
            break;
        //case 4://修改
        //    wm.Mod_Stu();
        //    break;
        //case 5://查找
        //    wm.Find_Stu();
        //    break;
        //    //case 6://排序
        //    //    wm.Sort_Stu();
        //    //    break;
        default:
            system("cls");
            break;            //清屏
        }
    }

    system("pause");
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 於黾 2022-12-27 11:38
    关注

    i < this->stuNum-1
    越界了

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

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月27日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效