-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovies.cpp
133 lines (118 loc) · 4.32 KB
/
movies.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <string>
struct Movies {
std::string title;
int year;
std::string genre;
int duration;
std::string rating;
};
// Maximum amount of movies user can enter
const int MAX_MOVIES = 50;
void addMovie(Movies movie[MAX_MOVIES], int& movieCount);
void showMovies(Movies movie[MAX_MOVIES], int movieCount);
void sortMovies(Movies movies[MAX_MOVIES], int movieCount);
void searchMovie(Movies movie[MAX_MOVIES], int movieCount);
int main() {
int option;
int movieCount = 0;
Movies movie[MAX_MOVIES];
do {
std::cout << "\t MENU \n";
std::cout << "1. Add movie\n";
std::cout << "2. Show movies\n";
std::cout << "3. Sort movies\n";
std::cout << "4. Search movie by genre\n";
std::cout << "5. Exit\n";
std::cin >> option;
switch (option) {
case 1:
addMovie(movie, movieCount);
break;
case 2:
showMovies(movie, movieCount);
break;
case 3:
sortMovies(movie, movieCount);
break;
case 4:
searchMovie(movie, movieCount);
break;
case 5:
std::cout << "Thank you for using!\n";
break;
}
} while (option != 5);
}
// Add a movie one by one keeping track of a counter
void addMovie(Movies movie[MAX_MOVIES], int &movieCount) {
if (movieCount < MAX_MOVIES) {
std::cin.ignore();
std::cout << "Enter the movie title:\n";
std::getline(std::cin, movie[movieCount].title);
std::cout << "Enter the release year of the movie:\n";
std::cin >> movie[movieCount].year;
std::cin.ignore();
std::cout << "Enter the genre of the movie:\n";
std::getline(std::cin, movie[movieCount].genre);
std::cout << "Enter the duration in minutes:\n";
std::cin >> movie[movieCount].duration;
std::cin.ignore();
std::string rating;
do {
std::cout << "Enter the rating of the movie (G, PG, PG-13, R, and NC-17):\n";
std::getline(std::cin, rating);
} while (rating != "G" && rating != "PG" && rating != "PG-13" && rating != "R" && rating != "NC-17");
movie[movieCount].rating = rating;
movieCount++;
} else {
std::cout << "The maximum number of movies has been entered.\n";
}
}
// iterate over to display the info of each movie
void showMovies(Movies movie[MAX_MOVIES], int movieCount) {
for (int i = 0; i < movieCount; i++) {
std::cout << "Movie # " << i + 1 << '\n';
std::cout << "Title: " << movie[i].title << '\n';
std::cout << "Release year: " << movie[i].year << '\n';
std::cout << "Genre: " << movie[i].genre << '\n';
std::cout << "Duration: " << movie[i].duration << '\n';
std::cout << "Rating: " << movie[i].rating << '\n';
std::cout << '\n';
}
}
// using bubble sort order the movies from top to bottom and display the title and year
void sortMovies(Movies movies[MAX_MOVIES], int movieCount) {
for (int i = 0; i < movieCount - 1; i++) {
for (int j = 0; j < movieCount - i - 1; j++) {
if (movies[j].year > movies[j + 1].year) {
Movies temp = movies[j];
movies[j] = movies[j + 1];
movies[j + 1] = temp;
}
}
}
for (int i = 0; i < movieCount; i++) {
std::cout << "Movie: " << movies[i].title << '\n';
std::cout << "Release year: " << movies[i].year << '\n';
std::cout << '\n';
}
}
// display all the genres avaible and then search for the one the user wants displaying the genre and the title of the movie
void searchMovie(Movies movie[MAX_MOVIES], int movieCount) {
std::cin.ignore();
std::string genreToSearch;
std::cout << "Available genres: \n";
for (int i = 0; i < movieCount; i++) {
std::cout << "- " << movie[i].genre << '\n';
}
std::cout << "Which genre do you want to search for: " << '\n';
getline(std::cin, genreToSearch);
for(int i = 0; i < movieCount; i++) {
if(genreToSearch == movie[i].genre) {
std::cout << "Movies with genre: " << movie[i].genre << "!\n";
std::cout << "Title: " << movie[i].title << '\n';
break;
}
}
}