Thread: program crashes while printing data from vectors

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    program crashes while printing data from vectors

    i have a localstudent and internationalstudent class inherit from student class. read and print are virtual functions. After i have set all the member variables when i wan to print out all the student information, my program crashes. Why and how to fix it?


    Code:
    int main()
    {
        clsUniversityProgram objProgram[3];
        for (int x = 0; x < 3; x++)
        {
            objProgram[x].programInfo();  // prompt user to enter program details and set them accordingly
        }
        vector <clsStudent*> student;
        addStudents(student, objProgram);
        student.front()->print();
    
        return 0;
    }
    addStudents function
    Code:
    void addStudents(vector <clsStudent*>& student, clsUniversityProgram objProgram[])
    {
        clsLocalStudent *studentadd;
        studentadd = new clsLocalStudent();
        studentadd->read(objProgram);
        student->push_back(studentadd);
    }
    read virtual function
    Code:
    void clsLocalStudent::read(clsUniversityProgram *objProgram)
    {
        int program, years, times;
        clsUniversityProgram selectProgram;
        clsDate mDOB;
        double mGPA;
        string studName, accNo, gender;
        float rate, principle;
    
    
        setNationality("Local");
    
    
        // prompt for student name
        cout << endl << "Student name\t\t: ";
        cin >> studName;
        setName(studName);
    
    
        // prompt for student DOB
        cout << endl << "Date of Birth\t\t: ";
        cin >> mDOB;
        setDateOfBirth(mDOB);
    
    
        cout << " +===========================+" << endl;
        cout << "  press 1 - for " << objProgram[0].getProgramName()
                            << "\n  press 2 - for " << objProgram[1].getProgramName()
                            << "\n  press 3 - for " << objProgram[2].getProgramName() << endl;
        cout << " +===========================+";
        cout << endl << "Program name\t\t: ";
        cin >> program;
    
    
         while (program != 1 && program != 2 && program != 3)
        {
            cout << "Invalid program! Please select key in 1, 2 or 3 only.";
            cin >> program;
        }
    
    
        if (program == 1)
        {
            setProgram(&objProgram[0]);
        }
    
    
        else if (program == 2)
        {
            setProgram(&objProgram[1]);
        }
    
    
        else
        {
            setProgram(&objProgram[2]);
        }
    
    
        clsAccount objAccount(accNo,studName,accID,mDOB, accStartDate, principle, rate, years, times);
        setAccounts(objAccount);
    
        // and setting other information
    }
    print virtual function
    Code:
    void clsLocalStudent::print()
    {
        cout << "\nStudent Name\t\t\t: " << getName()
            << "\nDate of Birth\t\t\t: " << getDateOfBirth()
            << "\nProgram Name\t\t\t: << getProgram()->getProgramName()
            << "\nAccount Number\t\t: << getAccounts()->getAccountNumber();
    }
    Last edited by Alexius Lim; 11-23-2013 at 11:06 PM.

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    59
    minor mistake while copying
    Code:
    void clsLocalStudent::print()
    {
        cout << "\nStudent Name\t\t\t: " << getName()
            << "\nDate of Birth\t\t\t: " << getDateOfBirth()
            << "\nProgram Name\t\t\t: "<< getProgram()->getProgramName()
            << "\nAccount Number\t\t: "<< getAccounts()->getAccountNumber();
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You fix it by running it in the debugger and waiting for it to crash.

    The debugger will catch the exception, and point you at a line of code.
    It will also give you a stack trace showing the function call hierarchy at that point. You can move through these stack frames to examine the program state at each function level.

    The object of the exercise is to start at the innermost function (where the crash actually occurred), then work your way back towards main(), looking for something which is wrong.
    Say for example
    - bad input data
    - erroneous calculations
    - uninitialised data
    - subscripts out of range
    etc etc.

    You can also use the debugger to do this process forwards.
    You start by setting a breakpoint at the start of main, and then step your way through the code until you see it doing something you didn't expect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Can I see the .h file too?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jocdrew21 View Post
    Can I see the .h file too?
    you can see whatever the compiler saw at the time that the program was built. gdb is a good command line based debugger that can show you the lines of code as you step through them. visual studio has an outstanding integrated debugger. I often wish GDB was as fast and well integrated as the visual studio debugger. many IDEs like eclipse, netbeans, and code::blocks do a pretty good job of integrating gdb.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-17-2012, 12:37 PM
  2. loading vectors and data before main
    By lawrenced in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2009, 01:42 PM
  3. Data Structure to store unlimited dynamic vectors
    By m3rk in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2009, 06:12 AM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. How to input a data file to parallel vectors
    By Wink- in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 05:24 PM

Tags for this Thread