简体   繁体   English

尝试将值添加到类中的数组(或向量)时,C ++程序崩溃

[英]C++ Program crashing when trying to add values to an array (or a vector) inside a class

I'm working on a school project - a library management system - and am encountering a problem. 我正在做一个学校项目-图书馆管理系统-遇到了问题。 The relevant code is as follows: 相关代码如下:

class student
{
    int stadmno;
    char stname[15];
    int bkcount;
    char Class[6];
    int vs[6];
    int i;

    public:
        student()
        {
            bkcount = 0;
            i = 0;
        }
        void getstu();                        //To input data.
        void dispstu();                       //To output data.
        int ret_stadmno()
        {
            return stadmno;
        }
        int ret_bkcount()
        {
            return bkcount;
        }
        char* ret_stname()
        {
            return stname;
        }
        void modify_stadmno()
        {
            cin>>stadmno;
            cin.get();
        }
        void modify_stname()
        {
            gets(stname);
        }
        void modify_bkcount()
        {
            cin>>bkcount;
            cin.get();
        }
        char* ret_class()
        {
            return Class;
        }
        void modify_class()
        {
            gets(Class);
        }
        void bk_borrow(int n)
        {
            cout<<"\n Check 1.";
            vs[i] = n;
            cout<<"\n Check 2.";
            i++;
        }
        int* ret_borrowed()
        {
            return vs;
        }
        void bk_return(int n)
        {
            int size = sizeof(vs)/sizeof(int);
            for(int j = 0 ; j<size ; j++)
            {
                if(vs[j] == n)
                    vs[j] = 0;
            }
        }
}s;

class book
{
    int bkno;
    char bkname[30];
    char auname[15];
    char genre[10];
    int state;
    int borrowed;

    public:
        book()
        {
            state = 0;
            borrowed = 0;
        }
        void getbk();                      //To input data.
        void dispbk();                     //To output data.
        char* ret_bkname()
        {
            return bkname;
        }
        char* ret_auname()
        {
            return auname;
        }
        int ret_bkno()
        {
            return bkno;
        }
        char* ret_genre()
        {
            return genre;
        }
        void modify_bkno()
        {
            cin>>bkno;
            cin.get();
        }
        void modify_bkname()
        {
            gets(bkname);
        }
        void modify_auname()
        {
            gets(auname);
        }
        void modify_genre()
        {
            gets(genre);
        }
        int check()
        {
            return state;
        }
        void issue(int n)
        {
            state = 1;
            borrowed = n;
        }
        void deposit()
        {
            state = 0;
            borrowed = 0;
        }
        int ret_borrowed()
        {
            return borrowed;
        }
}b;

void issue_book()
{
    clear();
    cout<<"\n\t\t Book Issue Screen. \n";
    int n;
    do
    {
        cout<<"\n Search according to: ";
        cout<<"\n\t 1. Title. ";
        cout<<"\n\t 2. Author. ";
        cout<<"\n\t 3. Book number. ";
        cout<<"\n\t 4. Genre. ";
        cout<<"\n\n Press 5 to exit. \n";

        cin>>n;
        cin.get();

        if(n==1 || n==2 || n==3 || n==4)
        {
            vector<int> vb;
            switch(n)
            {
                case 1: vb = search_bkname();
                    break;
                case 2: vb = search_auname();
                    break;
                case 3: vb = search_bkno(0);
                    break;
                case 4: vb = search_genre();
                    break;
            }

            fstream if1, if2;
            if1.open("Books.dat", ios::in | ios::binary);
            if2.open("Students.dat", ios::in | ios::binary);

            if(vb.back() == -1)
            {
                cout<<"\n Record not found. Please try again. \n";
            }
            else
            {
                vector<int> vs;
                int op, admno, z, c, no;
                z = 0;
                c = 0;
                cout<<"\n Search results: \n";
                book_header();
                for(int i = 0; i<vb.size(); i++)
                {
                    if1.seekg(vb[i], ios::beg);
                    if1.read((char*)&b, sizeof(b));
                    b.dispbk();
                }
                cout<<"\n Choose a book (Numerically): ";
                cin>>op;
                cin.get();
                if1.seekg(vb[op-1], ios::beg);
                if1.read((char*)&b, sizeof(b));
                if(b.check() == 1)
                {
                    cout<<"\n Book is currently borrowed.";
                    vs = search_stadmno(b.ret_borrowed());
                    if2.seekg(vs.back(), ios::beg);
                    if2.read((char*)&s, sizeof(s));
                    cout<<"\n Holder: "<<s.ret_stname()<<", "<<s.ret_stadmno()<<endl;
                    c = 1;
                }
                if(c == 0)
                {
                    do
                    {
                        cout<<"\n Enter the admission number of student borrowing the book: ";
                        cin>>admno;
                        cin.get();
                        vs = search_stadmno(admno);
                        if(vs.back() == -1)
                        {
                            cout<<"\n Record not found. Please try again. \n";
                            z = 1;
                        }
                        else
                        {
                            z = 0;
                            if2.seekg(vs.back(), ios::beg);
                            if2.read((char*)&s, sizeof(s));
                            student_header();
                            s.dispstu();
                            if1.seekg(vb[op-1], ios::beg);
                            if1.read((char*)&b, sizeof(b));
                            book_header();
                            b.dispbk();
                            no = b.ret_bkno();
                            s.bk_borrow(no);
                            cout<<"\n Check 3.";
                            b.issue(s.ret_stadmno());
                            cout<<"\n Book issued successfully. \n";
                        }
                    }while(z != 0);
                }
            }
            if1.close();
            if2.close();
        }
        else if(n != 5)
                cout<<"\n Invalid input. \n";
    }while(n != 5);
}

While executing the program, there are no errors, and the program outputs till Check 1. After that, it crashes, saying 'project.exe has stopped working'. 在执行程序时,没有错误,并且程序输出直到Check1。此后,它崩溃,说“ project.exe已停止工作”。 The same thing happens while using a vector. 使用向量时也会发生相同的情况。 Can someone please point out the problem? 有人可以指出问题吗?

PS: I'm using Dev-C++ 5.11. PS:我正在使用Dev-C ++ 5.11。
PPS: This is my first question on here, I'm sorry if something isn't right with the formatting. PPS:这是我的第一个问题,如果格式不正确,我感到抱歉。

Edit: Added code for both classes, and the function issue_book. 编辑:为两个类和功能issue_book添加了代码。

Thank you! 谢谢!

Without seeing your complete code, this is hard to tell. 没有看到完整的代码,这很难分辨。 But most likely, the assignment 但最有可能的是

vs[i] = n;

fails due to being out of bounds. 由于越界而失败。 You reserved vs as a fixed-size array of size 6, so i must be between 0 and 5 (inclusive). 您将vs保留为大小为6的固定大小的数组,因此i必须在0到5(含)之间。

Either attach a debugger and break on failure, or output i before the assignment to see what values it can take. 要么连接调试器并在失败时中断,要么在分配之前输出i以查看其可以获取的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM