Thread: How to fix this? pointer

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

    How to fix this? pointer

    in my main i have this. I have found out that my error is at the setProgram(&selectProgram) part but i do not know how to fixed it. when i try to print, my program crashes. read and print are virtual functions. I have student as base class and derived class localStudent.

    Code:
    int main() {  
    int input, program;
    
        for (int x = 0; x < 3; x++)
        {
            objProgram[x].programInfo();
        }
    
    cout << "Enter how many students to create: ";
        cin >> numStudents;
    
    clsStudent **student;
        student = new clsStudent *[numStudents];
    
    
        // loop the times of numStudents
        for (int i = 0; i < numStudents; i++)
        {
            cout << endl << " ========== Student " << i+1 << " Information ========== " << endl;
            cout << " +===============================+";
            cout << "\n  press 1 - for local \n  press 2 - for international" << endl;
            cout << " +===============================+";
            cout << endl;
    
    
            cout << "Nationality: ";
            cin >> input;
    
    for (int i = 0; i < numStudents; i++)
        {
            cout << endl << " ========== Student " << i+1 << " Information ========== " << endl;
            cout << "\npress 1 - for local \npress 2 - for international)\nNationality: ";
            cin >> input;
    
            while (input != 1 && input != 2)
            {
                cout << "Invalid input! Please select key in 1 or 2 only.";
                cout << "\npress 1 - for local \npress 2 - for international)\nNationality: ";
                cin >> input;
            }
    
    
            // if input = 1, student is local
            if (input == 1)
            {
                student[i] = new clsLocalStudent();        
                student[i]->read(objProgram);
                student[i]->print();
            }
    }
    read function
    Code:
    void clsLocalStudent::read(clsUniversityProgram *objProgram)
    {
        clsUniversityProgram selectProgram;
    
    
        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;
    
    
        // loop while program selected is invalid
        while (program != 1 && program != 2 && program != 3)
        {
            cout << "Invalid program! Please select key in 1, 2 or 3 only.";
            cin >> program;
        }
    
    
        if (program == 1)
        {
            selectProgram = objProgram[0];
        }
    
    
        // excluded if program == 2, program == 3(same thing) 
    
    
        setProgram(&selectProgram);
    }
    student.h
    Code:
    class clsStudent
    {
    private:
         clsUniversityProgram *programEnrolled;      
    public:
        clsUniversityProgram* getProgram(void) const;
        void setAccounts(clsAccount *argAccounts);
    }
    student.cpp

    Code:
    void clsStudent::setProgram(clsUniversityProgram *argProgram)
    {
        programEnrolled = argProgram;
    }
    
    
    clsUniversityProgram* clsStudent::getProgram(void) const
    {
        return programEnrolled;
    }

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    59
    clsUniversityProgram has string programName.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    OK well...
    Code:
        if (program == 1)
        {
            selectProgram = objProgram[0];
        }
     
        // excluded if program == 2, program == 3(same thing)
     
        setProgram(&selectProgram);
    selectProgram is a local variable, so as a consequence when the function ends, the value will not be accessible (in a well-defined fashion). When you assigned the address of selectProgram to member data of the current object through setProgram, you created a pointer to a local variable. That's what's wrong, and the only way to fix it is to avoid doing that. You might want to rethink your design so that it does not involve so many pointers. For example, why is the university program for each student a pointer?

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    59
    because my programs will ask the user for all the program information. then when the user is prompt to select program he will only choose 1, 2 or 3. so the name, code and fees (which are not included here) will be auto set corresponding to the student. Or if i wan to change the name of the program, then all the program name for each student will change accordingly, hence i do not need to change the program details for each student one by one.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Well, at least there was a reason for this.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    59
    Quote Originally Posted by whiteflags View Post
    OK well...
    selectProgram is a local variable, so as a consequence when the function ends, the value will not be accessible (in a well-defined fashion). When you assigned the address of selectProgram to member data of the current object through setProgram, you created a pointer to a local variable. That's what's wrong, and the only way to fix it is to avoid doing that. You might want to rethink your design so that it does not involve so many pointers. For example, why is the university program for each student a pointer?
    i'll fixed the problem. change my code to this =)
    Code:
    setProgram(&objProgram[0]);
    thanks for the explaination

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2013, 09:33 PM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread