-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.cpp
More file actions
321 lines (253 loc) · 6.96 KB
/
assignment3.cpp
File metadata and controls
321 lines (253 loc) · 6.96 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Date: 5/4/2019
This program is creating a movie database by reading information in file.
Then, user can search it by entering filename.*/
#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
struct Movie {
string title;
double gross_total;
string director;
string release_date;
int runtime;
};
int numberOfLines(ifstream&);
void populateMovieFromFile(ifstream&, Movie&);
void displayMovie(const Movie&);
Movie* createDatabase(int&);
bool caseInsensitiveCmp(string, string);
void findMovie(Movie*, int);
void saveToFile(const Movie&);
bool promptToContinue();
void displayFavorites();
int main() {
int num_movies;
Movie *ptr;
bool exit;
ptr = createDatabase(num_movies);
do {
findMovie(ptr, num_movies);
exit = promptToContinue();
} while (exit == 1);
displayFavorites();
delete[] ptr;
ptr = nullptr;
return 0;
}
/*
1- the function receives an input file.
2- returns an integer representing the number of lines in the file.
3- it counts how many movoies in the file.
*/
int numberOfLines(ifstream &inFile) {
int count = 0;
string temp;
while (getline(inFile, temp))
count++;
inFile.clear();
inFile.seekg(0, ios::beg);
return count;
}
/*
1- The function receives an input file and a Movie object.
2- returns nothing.
3- it builds different kinds of info of a movie to a Movie object.
*/
void populateMovieFromFile(ifstream &inFile, Movie &object) {
string str;
getline(inFile, str);
int pos1 = str.find(',', 0);
object.title = str.substr(0, pos1);
int pos2 = str.find(',', pos1 + 1);
object.gross_total = stod(str.substr(pos1 + 2, pos2 - pos1 - 2));
int pos3 = str.find(',', pos2 + 1);
object.director = str.substr(pos2 + 2, pos3 - pos2 - 2);
int pos4 = str.find(',', pos3 + 1);
object.release_date = str.substr(pos3 + 2, pos4 - pos3 - 2);
object.runtime = stoi(str.substr(pos4 + 2, str.size() - pos4 - 1));
}
/*
1- the function receives a Movie object passed by constant reference.
2- returns nothing.
3- it displays the contents of that object in a formatted fashion.
*/
void displayMovie(const Movie& object) {
cout << endl;
cout << setw(14) << "Title: " << object.title << endl;
cout << setw(14) << "Gross Total: " << object.gross_total
<< " billion dollars" << endl;
cout << setw(14) << "Director: " << object.director << endl;
cout << setw(14) << "Release Date: " << object.release_date << endl;
cout << setw(14) << "Runtime: " << object.runtime << " minutes" << endl;
}
/*
1- the function receives the number of movies in the database by reference.
2- returns a pointer pointing to a array of Movie objects.
3- it creats the movie database.
*/
Movie* createDatabase(int& num_movies) {
ifstream inFile;
string filename;
do {
cout << "Please enter filename: ";
getline(cin, filename);
inFile.open(filename);
if (!inFile)
cout << "The filename is invalid, please enter it again.\n";
} while (!inFile);
num_movies = numberOfLines(inFile);
Movie *list = new Movie[num_movies];
for (int i = 0; i < num_movies; i++) {
populateMovieFromFile(inFile, list[i]);
}
inFile.close();
return list;
}
/*
1- the function receives two strings.
2- returns true or false.
3- it performs a noncase-sensitive comparison on the two strings.
*/
bool caseInsensitiveCmp(string title, string title_to_search) {
int x = 0;
while (title[x] != '\0') {
title[x] = tolower(title[x]);
x++;
}
int y = 0;
while (title_to_search[y] != '\0') {
title_to_search[y] = tolower(title_to_search[y]);
y++;
}
if (title == title_to_search)
return 1;
else
return 0;
}
/*
1- the function receives a pointer to an array of Movie objects
and number of Movies.
2- returns nothing.
3- it finds if any movie in database matches with the filename entered by user.
*/
void findMovie(Movie* list, int num_movies) {
string title_to_search, save;
bool find;
int sub = 0;
cout << "\nEnter a movie title to search for: ";
getline(cin, title_to_search);
for (; sub < num_movies; sub++) {
find = list[sub].title.size() == title_to_search.size() ? 1 : 0;
if (find) {
find = caseInsensitiveCmp(list[sub].title, title_to_search);
}
if (find)
break;
}
if (find) {
displayMovie(list[sub]);
do {
cout << "\nWould you like to save the movie? (Y or N ): ";
getline(cin, save);
} while (!(save == "y" || save == "Y" || save == "n" || save == "N"));
if (save == "y" || save == "Y")
saveToFile(list[sub]);
}
else
cout << "\nSorry, we didn't find the filename you entered." << endl;
}
/*
1- the function receives a Movie object, passed by constant reference.
2- returns nothing.
3- it saves the Movie object to the “favorites.txt” file.
*/
void saveToFile(const Movie& object) {
ofstream outFile;
outFile.open("favorites.txt", ios::app);
outFile << object.title << ", " << object.gross_total << ", "
<< object.director << ", " << object.release_date
<< ", " << object.runtime << endl;
if (outFile)
cout << "Successfully saved to the favorites.txt!" << endl;
else
cout << "Unsuccessfully saved!" << endl;
outFile.close();
}
/*
1- the function receives nothing.
2- returns nothing.
3- it will ask user wether wants to continue the program or not.
*/
bool promptToContinue() {
string cont;
do {
cout << "\nWould you like to exit? (Y/N) ";
cin >> cont;
} while (!(cont == "y" || cont == "Y" || cont == "n" || cont == "N"));
cin.ignore();
if (cont == "Y" || cont == "y")
return 0;
else
return 1;
}
/*
1- the function receives nothing.
2- returns nothing.
3- it will display all saved favorite movies.
*/
void displayFavorites() {
ifstream input;
input.open("favorites.txt");
if (!input)
cout << "\nThere's no favorite saved." << endl;
else {
int num_favorites;
num_favorites = numberOfLines(input);
Movie *favorite = new Movie[num_favorites];
cout << "\nYour saved movies are:" << endl;
for (int i = 0; i < num_favorites; i++) {
populateMovieFromFile(input, favorite[i]);
displayMovie(favorite[i]);
}
delete[] favorite;
favorite = nullptr;
}
input.close();
}
/* Sample Run:
Please enter filename: movie.txt
Enter a movie title to search for: AvaTar
Title: Avatar
Gross Total: 2.788 billion dollars
Director: James Cameron
Release Date: 12/18/09
Runtime: 161 minutes
Would you like to save the movie? (Y or N ): y
Successfully saved to the favorites.txt!
Would you like to exit? (Y/N) n
Enter a movie title to search for: FINDING dory
Title: Finding Dory
Gross Total: 1.029 billion dollars
Director: Andrew Stanton
Release Date: 6/17/16
Runtime: 97 minutes
Would you like to save the movie? (Y or N ): Y
Successfully saved to the favorites.txt!
Would you like to exit? (Y/N) N
Enter a movie title to search for: jurassic word
Sorry, we didn't find the filename you entered.
Would you like to exit? (Y/N) y
Your saved movies are:
Title: Avatar
Gross Total: 2.788 billion dollars
Director: James Cameron
Release Date: 12/18/09
Runtime: 161 minutes
Title: Finding Dory
Gross Total: 1.029 billion dollars
Director: Andrew Stanton
Release Date: 6/17/16
Runtime: 97 minutes
*/