The problem I am having is that the which movies to view function is supposed to ask the user how they want the titles displayed (view all or by genre). Then the get_movies function is supposed to display the titles based on how the user answered the question in the which movies to view function. Below is how I have the code for the two functions currently, but it isn't right. I don't know how to link the 'answer' from which movies to view function to the get_movies function. How can I do this? Should I declare viewchoice as a global variable? in order to use answer in both functions?
I can post entire program if needed, but my problem relates specifically to these two functions:
//ask user which movies he wants to view, ask if they want to view all or by genre
void which_movies_to_view(movie_record movies[])
{
int viewchoice;
int genrechoice;
do
{
cout<< "View all movies or by genre (press 1 for view all or 2 for by genre or 3 for exit: "<< endl;
cin>> viewchoice;
if (viewchoice == 1)
{
cout<< "These are all the movies available currently: "<< endl;
cout << movies[0].title << endl;
cout << movies[1].title << endl;
cout << movies[2].title << endl;
cout << movies[3].title << endl;
cout << movies[4].title << endl;
}
elseif(viewchoice == 2)
{
do
{
cout<< "Which genre do you want to view: kids (1),family (2), comedy (3), thriller (4), exit (5): "<< endl;
cin>> genrechoice;
cout << endl;
if(genrechoice == 1)
{
cout << movies[0].title << endl;
}
elseif (genrechoice == 2)
{
cout << movies[2].title << endl;
}
elseif (genrechoice == 3)
{
cout << movies[1].title << endl << movies[4].title << endl;
}
elseif (genrechoice == 4)
{
cout << movies[3].title << endl;
}
}while(genrechoice != 5);
}
}while(viewchoice != 3);
}
// this function should display the titles of the movies dependant one
// how user answer to view all or by genre in the function above.
void get_movies(movie_record movies[])
{
int userchoice;
do{
cout<< "Please choose a movie to view (1 - 5) exit(6): "<< endl;
cin>>userchoice;
if(userchoice == 1)
{
cout << movies[0].title << endl;
}
if(userchoice == 2)
{
cout << movies[1].title << endl;
}
if(userchoice == 3)
{
cout << movies[2].title << endl;
}
if(userchoice == 4)
{
cout << movies[3].title << endl;
}
if(userchoice == 5)
{
cout << movies[4].title << endl;
}
}while(userchoice != 6);
}
//prototype
vector<movie_record> filter(const vector<movie_record>&, const genre_type&);
//emplementation
vector<movie_record> filter(const vector<movie_record>& movies, const genre_type& genre)
{
//vector. this is where we will store our movies of a single genre
vector<movie_record> filtered_movies;
//iterate through the list
for(vector<movie_record>::const_iterator it = movies.begin(); it != movies.end(); ++it)
{
//if we cross a record of the genre we want to see, add it to the list
if(it->genre == genre)
{
filtered_movies.push_back(*it);
}
}
//now we have a single genre of movies to show.
return filtered_movies;
}
and now you have a list of movies that consists of a single genre. Note that you will need to at a new piece of data if it isn't already there. You need to be able to identify the genre of a movie_record and this means a new piece of data must be added that identifies it as such.
/prototype
vector<movie_record> Filter(const vector<movie_record>&, const genre_type&);
//emplementation
vector<movie_record> Filter(const vector<movie_record>& movies, const genre_type& genre)
{
//vector. this is where we will store our movies of a single genre
vector<movie_record> filtered_movies;
//iterate through the list
for(int i = 0; i < movies.size(); i++)
{
//if we cross a record of the genre we want to see, add it to the list
if(movies[i] == genre)
{
filtered_movies[i] = movies[i];
}
}
//now we have a single genre of movies to show.
return filtered_movies;
}
//ask user which movies he wants to view, ask if they want to view all or by genre
void which_movies_to_view(movie_record movies[])
{
int viewchoice;
int genrechoice;
do
{
cout<< "View all movies or by genre (press 1 for view all or 2 for by genre or 3 for exit: "<< endl;
cin>> viewchoice;
if (viewchoice == 1)
{
cout<< "These are all the movies available currently: "<< endl;
for(int i=0;i<NUM_MOVIES;i++)
{
get_movies(movies,i+1);
}
}
elseif(viewchoice == 2)
{
do
{
cout<< "Which genre do you want to view: kids (1),family (2), comedy (3), thriller (4), exit (5): "<< endl;
cin>> genrechoice;
cout << endl;
// Kids
if(genrechoice == 1)
{
for(int i=0;i<NUM_MOVIES;i++)
{
if(movies[i].genre == "Kids")
get_movies(movies,i+1);
}
}
// Familly
elseif (genrechoice == 2)
{
for(int i=0;i<NUM_MOVIES;i++)
{
if(movies[i].genre == "Family")
get_movies(movies,i+1);
}
}
// Comedy
elseif (genrechoice == 3)
{
for(int i=0;i<NUM_MOVIES;i++)
{
if(movies[i].genre == "Comedy")
get_movies(movies,i+1);
}
}
// Thriller
elseif (genrechoice == 4)
{
for(int i=0;i<NUM_MOVIES;i++)
{
if(movies[i].genre == "Thriller")
get_movies(movies,i+1);
}
}
}while(genrechoice != 5);
}
}while(viewchoice != 3);
}
// this function should display the titles of the movies dependant one
// how user answer to view all or by genre in the function above.
void get_movies(movie_record movies[], int userchoice)
{
if(userchoice == 1)
{
cout << movies[0].title << endl;
}
if(userchoice == 2)
{
cout << movies[1].title << endl;
}
if(userchoice == 3)
{
cout << movies[2].title << endl;
}
if(userchoice == 4)
{
cout << movies[3].title << endl;
}
if(userchoice == 5)
{
cout << movies[4].title << endl;
}
}
But still even with this. The instructions are to have the which_movies_to_view function is only to ask the user for a filter to view all or by genre.
And the get_movies function is to display the movie titles based on how the user answered the which_movies_to_view.
Something is still not right. And I am getting an infinite loop on top of that as well....
YOU HAVE TO ADD GENRE as a member of the movie_record structure... you can't make a boolean comparison of two things that don't have operators to handle them.
@afleury1
initiate your integers, and reset them before input.
Also, stop using elseif statements.... the compiler can confuse multiple nested else/if statements in some cases. I stay away from them. You will need to add continue at the end of the if statements though.