XPekez
XPekez

Reputation: 11

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'. The same thing happens while using a vector. Can someone please point out the problem?

PS: I'm using Dev-C++ 5.11.
PPS: This is my first question on here, I'm sorry if something isn't right with the formatting.

Edit: Added code for both classes, and the function issue_book.

Thank you!

Upvotes: 0

Views: 129

Answers (1)

Demosthenes
Demosthenes

Reputation: 1535

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).

Either attach a debugger and break on failure, or output i before the assignment to see what values it can take.

Upvotes: 5

Related Questions