Structs program problems

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//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;
		 }
    
        else if(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;
                 }
                else if (genrechoice == 2)
                {
                     cout << movies[2].title << endl;
                }
                else if (genrechoice == 3)
                {
                     cout << movies[1].title << endl << movies[4].title << endl;
                }
                else if (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);

}
ok, do you know how to write classes?

also, the solution is quite simple: write a function to single out a genre.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//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.
Last edited on
Rewritten in a more familar array access format for TS.

/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/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;
}
Well we are supposed to be using arrays and structs to create the program , we haven't been taught anything about vectors....yet.

I have altered the code a bit to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//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);
			}
		 }
    
        else if(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
                else if (genrechoice == 2)
                {
                      for(int i=0;i<NUM_MOVIES;i++)
					   {
					   if(movies[i].genre == "Family")
				            get_movies(movies,i+1);
					   }
                }

				// Comedy
                else if (genrechoice == 3)
                {
                       for(int i=0;i<NUM_MOVIES;i++)
					   {
					   if(movies[i].genre == "Comedy")
				            get_movies(movies,i+1);
					   }
                }

				// Thriller
                else if (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....
@IceThatJaw

CORRECTION line 14: if(movies[i].genre == genre)

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 else if 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.
Last edited on
Topic archived. No new replies allowed.