0

At the moment my program is crashing after displaying one line out of the 250 it is supposed to display. Here is my code:

string MovieList::PrintAll() {
   for (int i = 0; i < last_movie_index; i++) {
        movies[last_movie_index].Movie::PrintMovie();
   }
}

string Movie::PrintMovie() {
    cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
            year_made << ")" << "\t" << name << endl;
}

Full Movie and MovieList class:

class Movie {
public:
    Movie();
    Movie(string n, int y, int v, double r);
    string get_name();
    void set_name(string n);
    int get_year();
    void set_year(int y);
    int get_votes();
    void set_votes(int v);
    double get_rating();
    void set_rating(double r);
    string PrintMovie();

private:
    string name;
    int year_made;
    int votes;
    double rating;

};

Movie::Movie() {
    name = "null";
    year_made = 0;
    votes = 0;
    rating = 0.0;
}

Movie::Movie(string n, int y, int v, double r) {
    name = n;
    year_made = y;
    votes = v;
    rating = r;
}

string Movie::get_name() {
    return name;
}

void Movie::set_name(string n) {
    name = n;
}

int Movie::get_year() {
    return year_made;
}

void Movie::set_year(int y) {
    year_made = y;
}

int Movie::get_votes() {
    return votes;
}

void Movie::set_votes(int v) {
    votes = v;
}

double Movie::get_rating() {
    return rating;
}

void Movie::set_rating(double r) {
    rating = r;
}

string Movie::PrintMovie() {
    cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
            year_made << ")" << "\t" << name << endl;
}

class MovieList {
public:
    MovieList(int size);
    ~MovieList();
    int Length();
    bool IsFull();
    void Add(Movie const& m);
    string PrintAll();

private:
    Movie* movies;
    int last_movie_index;
    int movies_size;
    int movie_count = 0;

};

MovieList::MovieList(int size) {
    movies_size = size;
    movies = new Movie[movies_size];
    last_movie_index = -1;
}

MovieList::~MovieList() {
    delete [] movies;
}

int MovieList::Length() {
    return last_movie_index;
}

bool MovieList::IsFull() {
    return last_movie_index == movies_size;
}

void MovieList::Add(Movie const& m)
{
    if (IsFull()) {
        cout << "Cannot add movie, list is full" << endl;
        return;
    }
    last_movie_index++;
    movies[last_movie_index] = m;
}

string MovieList::PrintAll() {
   for (int i = 0; i < last_movie_index; i++) {
        movies[i].Movie::PrintMovie();
   }
}

My array movies is dynamically allocated (i.e movies = new Movie[movies_size];). I noticed that using cout << movies[1] << endl will not work in the PrintAll function. Is this why it is crashing possibly? And what can I do to fix it?

andayn
  • 97
  • 1
  • 1
  • 10
  • 2
    should't it be _moves[i]_ instead of _moves[last_movie_index]_ in the loop? – alexm Dec 05 '14 at 23:30
  • @alexm Ahhh yes I feel like an idiot for that one. Unfortunately it is still crashing right after the first print. Now what will happen is it will print: 9.2 453500 (1994) The Shawshank Redemption 0.0 13483293892 (-5963777736) and then it will crash on me still – andayn Dec 05 '14 at 23:35
  • Make sure you are initializing the members of the Movie class. It looks like `votes` and `year_made` are not initialized from that Shawshank Redemption output line. It would make it easier if you posted the definition of that class. Also, there's no reason to include the `Movie::` qualification when calling the `PrintMovie()` function. – Wes Cumberland Dec 05 '14 at 23:38
  • @WesCumberland I edited the OT and posted my full `Movie` and `MovieList` class if that helps. The `votes` and `year_made` are initializing on The Shawshank Redemption, its right after that where everything goes haywire before crashing. – andayn Dec 05 '14 at 23:47
  • You've already been advised in another question that if your function returns a value, you should have a `return` statement. *(Rare standards-compliant exception: `int main()` doesn't have to have a return statement and defaults to returning zero. Why? 'Because'--that's why.)* So you've got string-returning `PrintAll` and `PrintMovie` functions with no return statement. Please enable and read warnings for not all paths returning a value, [as you were also advised](http://stackoverflow.com/questions/27325269/#comment43110236_27325269) – HostileFork says dont trust SE Dec 05 '14 at 23:50
  • What is the code where you are actually adding the Movies to the MovieList? – David Woo Dec 06 '14 at 00:00
  • Your last_movie_index is set up incorrectly. Either start it on zero and increment after assigning movie in MovieList::Add(). Or in MovieList::IsFull test against movies_size -1 – David Woo Dec 06 '14 at 00:08
  • This won't solve your problem, but if you want to make `cout << movies[i] << endl` work, then you need to define the following function: `friend ostream& operator<<(ostream& ostr, const Movie& movie){ string Movie::PrintMovie() { ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" << year_made << ")" << "\t" << name ; }` – Wes Cumberland Dec 06 '14 at 00:09

1 Answers1

0

This won't solve your problem, but if you want to make cout << movies[i] << endl work, then you need to define the following function in the Movie class:

friend ostream& operator<<(ostream& ostr, const Movie& movie){
    ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
            year_made << ")" << "\t" << name ;
}
Wes Cumberland
  • 1,318
  • 8
  • 12