C++ MovieList array and pointer -
i'm still bit stuck on part on assignment.
here prompt asking:
now can modify loadmovies function create movielist object , add each of movie objects it. function loadmovies should return pointer movielist object. means need create movielist object dynamically , on heap.
change main function , store returned movielist pointer in variable. test if works expected, can use printall function of movielist object.
here code far:
class movielist { public: movie* movies; int last_movie_index; int movies_size; int movie_count = 0; movielist(int size) { movies_size = size; movies = new movie[movies_size]; last_movie_index = -1; } ~movielist() { delete [] movies; } int length() { return movie_count; } bool isfull() { return movie_count == movies_size; } void add(movie const& m) { if (isfull()) { cout << "cannot add movie, list full" << endl; return; } ++last_movie_index; movies[last_movie_index] = m; } void printall() { (int = 0; < movie_count; i++) { movies[last_movie_index].printmovie(); } } }; void readmoviefile(vector<string> &movies); void loadmovies(); enum moviesortorder { by_year = 0, by_name = 1, by_votes = 2 }; int main() { loadmovies(); // todo: // need implement movie , movielist classes , // methods below program produce // output described in assignment. // // once have implemented everything, should able // uncomment code below , run program. movielist *movies = loadmovies(); // // test methods movie , movielist classes //printallmoviesmadeinyear(movies, 1984); //printallmovieswithstartletter(movies, 'b'); //printalltopnmovies(movies, 5); //delete movies; return 0; } void loadmovies() { vector<string> movies; readmoviefile(movies); string name; int year; double rating; int votes; (int = 0; < movies.size(); i++) { istringstream input_string(movies[i]); getline(input_string, name, '\t'); input_string >> year >> rating >> votes; movie movie (name, year, votes, rating); movie.printmovie(); } }
now i'm stuck @ professor asks me modify loadmovies in prompt , turn pointer. i'm drawing blanks. reason if try compile says:
c:\users\andy\documents\c++ homework\moviestatisticsprogram\moviestatsprogram.cpp:163: error: void value not ignored ought movielist *movies = loadmovies(); ^
the order of constructor wrong
movielist(int size) { movies = new int[movies_size]; // movies_size hasn't been initialized yet! movies_size = size; last_movie_index = -1; }
it should be
movielist(int size) : movies_size{size}, movies{new int[size]}, last_movie_index{0} {}
though @scottmcp-mvp noted array should be
movie* movie;
so constuctor be
movielist(int size) : movies_size{size}, movies{new movie[size]}, last_movie_index{0} {}
some advice getting started on remaining functions
length
function return how many current used last_movie_index
.
isfull
check if last_movie_index == movies_size - 1
add
need use last_movie_index
figure out element in array store movie.
printall
have iterate [0]
[movie_count]
, print out each element.
your add
function like
void movielist::add(movie const& m) { if (isfull()) { std::cout << "cannot add movie, list full" << std::endl; return; } movies[last_movie_index] = m; // assigns copy array ++last_movie_index; // increment movie index }
Comments
Post a Comment