Allen StCharles
Allen StCharles

Reputation: 1

Dynamic array of classes within a class

I am trying to build a class with a dynamic array of another class' objects within it. The base outer class is Sport, the secondary (inside in an array) is Player. I had some problems getting the add function to work and now that I finally have it working (I thought) I am getting errors from the display. When I call the Display function which is part of the Player class I am getting a read error. I'll post the biggest piece of code here and if anyone notices wtf i going wrong please let me know asap. I need tog et this working asap, the assignment is long past due and future assignments help build on top of it. I don't just need a working version I need to understand what is going wrong.

#include "Sport.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

// since we're going to call these from within the class
// consider making all but DisplayMenu() private

Sport::Sport() : array(0),length(0)
{

}

Sport::~Sport()
{

}

void Sport::DisplayMenu()
{
    bool exit(false);
    char entry(0);

    while(exit != true) // clarity
    {
    cout << "\nOREGON INSTITUTE OF TECHNOLOGY\n" << endl;
    cout << "  A - Add Player" << endl;
    cout << "  S - Search/Display a Player" << endl;
    cout << "  D - Display all Players" << endl;
    cout << "  C - Display Current Count of Players" << endl;
    cout << "  E - Exit/n" << endl;
    cin >> entry;

    switch (entry)
    {
        case 'A' :
            Add();
            break;

        case 'S' :
            Search();
            break;

        case 'D' :
            List();
            break;

        case 'C' :
            cout << "Currently " << length << " Players.";
            break;

        case 'E' :
            exit = true;
            break;

        default :

            break;
    }

}
}

void Sport::Add() //have array[] and length
{
Player **temp = new Player *[length+1];

for (int i = 0; i < length; i++)
{
    temp[i] = array[i];
}
temp[length] = &PromptUser();
length++;

delete [] array;
array = temp;
}

void Sport::List()
{
for (int i = 0; i < length; i++)
    (*array)[i].Display(); // <---this line is crashing the program.
}

void Sport::Search() const
{

}

Player Sport::PromptUser()
{
char name[25];
cout << "Enter name: ";
cin >> name;

int grade(0);
cout << "Enter grade: ";
cin >> grade;

double gpa(0.0);
cout << "Enter gpa: ";
cin >> gpa;

Player result(name, grade, gpa);
return result;
}

Upvotes: 0

Views: 700

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

With the line

temp[length] = &PromptUser();

you are taking the address of a temporary. That object will immediately be destroyed and you'll be left pointing to an invalid object. You need to make a permanent object somehow. For example:

temp[length] = new Player(PromptUser());

Just don't forget to delete it.

Even better -- don't use primitive arrays like this at all. Use a container like std::vector.

Upvotes: 3

Related Questions