C++ Program crashing from print function -
at moment program crashing after displaying 1 line out of 250 supposed display. here code:
string movielist::printall() { (int = 0; < 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 , 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 full" << endl; return; } last_movie_index++; movies[last_movie_index] = m; } string movielist::printall() { (int = 0; < last_movie_index; i++) { movies[i].movie::printmovie(); } }
my array movies dynamically allocated (i.e movies = new movie[movies_size];
). noticed using cout << movies[1] << endl
not work in printall function. why crashing possibly? , can fix it?
this won't solve problem, if want make cout << movies[i] << endl
work, need define following function in movie class:
friend ostream& operator<<(ostream& ostr, const movie& movie){ ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" << year_made << ")" << "\t" << name ; }
Comments
Post a Comment