0

I am attempting to create a class of MovieList that contains a dynamically allocated double [] of weeklyRevenue. I keep getting this error, I know it is associated with memory allocation, but not sure about the specifics. How can I fix this?

class Movie
{
public:

        Movie();
        void setMovieData(char [],char [],char, int,int,int,int,double[]);
        ~Movie();
        string getTitle() const;
        char getGenre() const;
        void display() const;    
        double getBoxOffice();
        private:
        char title[75];
        char director[50];
        char genre;
        Date releaseDate;
        double* weeklyRevenue;
        double* weeklyTake;
        int numWeeks;
};

class MovieList
{
 public:

        MovieList();    //constuctor

        ~MovieList();   //deconstructor

        int getCapacity();

        int getCurrentSize();

        void addMovie(char[], char[], char, int ,int, int,int, double[]);

        void insertMovie(Movie, int);

        void deleteMovie(char []);

        void displayList() const;

        void displayByGenre(char) const;

        int search(char []) const;

        Movie* movieListObj;

        private:
        int capacity, currentSize;
}

movieList.cpp:

    void Movie::setMovieData(char t[], char dir[], char g, int month, int day, int year,int numWks, double wklyTake[])

    {

    for(int i =0; i < 75; i++)

                title[i] = t[i];

            for (int i = 0; i < 50; i++)

                    director[i] = dir[i];

            genre = g;

            releaseDate.setDate(month,day,year);

            numWeeks = numWks;

            weeklyRevenue = new double[numWeeks];

            wklyTake = new double[numWeeks];

            for (int i = 0; i < numWeeks; i++)

                    weeklyRevenue[i] = wklyTake[i];
    }

    Movie::~Movie()
    {

            delete [] weeklyTake;

            weeklyTake = NULL;

            delete [] weeklyRevenue;

            weeklyRevenue = NULL;
    }
    void Movie::display() const
    {
            cout<<"Title: ";

            for(int i =0; i < 75; i++)

                    cout<< title[i];

            cout << "\nDirector: ";

            for (int i = 0; i < 50; i++)

                    cout << director[i];

        cout << "\nGenre:" << genre<<endl;

        cout << "Release Date: ";

        releaseDate.print();

        cout<<"Weeks since Release: " << numWeeks<<endl;

        cout <<"Box office: " ;

        double BoxOffice = 0;

        for(int i =0; i <numWeeks; i++)

                BoxOffice += weeklyRevenue[i];

        cout << BoxOffice;


}

MovieList::MovieList()
{
        capacity = 5;

        currentSize = 0;

        movieListObj = new Movie[currentSize];

}


MovieList::~MovieList()
{
        delete [] movieListObj;

        movieListObj = NULL;

}
void MovieList::addMovie(char t[], char dir[], char g, int month, int day, int year, int numWeeks, double* weeklyRevenue)
{
        if( currentSize >= capacity)

        {

                capacity += 5;

        }

        movieListObj[currentSize].setMovieData(t, dir, g, month, day, year, numWeeks, weeklyRevenue);

        currentSize++;

}

void MovieList::insertMovie(Movie movieList, int position)
{
        if(position < 0 || position > capacity)

                cout << "insert failed";

        else


        {

                movieListObj[position] = movieList;

                cout << "Insert successful";

        }


}

void MovieList::deleteMovie(char titleDel[])

{
        for( int i= 0; i < currentSize; i++)

        {
                while( titleDel == movieListObj[i].getTitle())

                        cout<< "Delete"  ;


        }
}

I am attempting to add a movie, but once I enter all data in the addMovie function I get the error "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Abort"

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 2
    Attaching a debugger should let you see which line leads to the exception being thrown, and should let you inspect nearby variables. This should be your first step whenever your program unexpectedly fails. – François Andrieux Jun 25 '19 at 14:14
  • The `capacity += 5;` line seems to be missing actually increasing the capacity of the moveListObj. A `std::vector` would be a much better container. – Eljay Jun 25 '19 at 14:29
  • Related: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – UKMonkey Jun 25 '19 at 14:39

0 Answers0