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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#define MAX_MOVIES 100
using namespace std;
enum {ADD = 1,PRINT, SEARCH, EXIT};
struct Movie
{
string nameOfMovie;
int scoreOfMovie;
};
void LoadFile(Movie* moviePtr[], int& count);
void ReadChoice (int& choice);
void DisplayMenu ();
void ProcessChoice (Movie* movie[], int& count, int choice);
void CheckInput (string prompt, int& num);
void AddMovie (Movie movie[], int& count);
bool ScoreValid (int score);
void Print (Movie const moviePtr[], int count);
void SearchMovie (Movie const movie[], int count);
string Tomato (int score);
void SaveFile(Movie* movie[], int count);
void DeletePtr(Movie* moviePtr[], int count);
void PrintLine (int size, char ch);
// Function: main
// Purpose: To add, search, and store names of movies with their rating and assigning them a FRESH or ROTTEN tag based on rating.
// Input: movies, count, choice
// Output: movies
// In Param: none
// Out Param: none
// Return: 0
// Function Called: LoadFile, ReadChoice, ProcessChoice, SaveFile
// Error Messages: none
int main ()
{
Movie *moviePtr[MAX_MOVIES] = {0};
*moviePtr = new Movie[MAX_MOVIES];
int count = 0;
int choice;
LoadFile(moviePtr, count);
ReadChoice(choice);
if (*moviePtr != 0)
while(choice != EXIT)
{
ProcessChoice(moviePtr, count, choice);
ReadChoice(choice);
}
SaveFile(moviePtr, count);
DeletePtr(moviePtr, count);
return 0;
}
// Function: LoadFile
// Purpose: To load, a pre-existing file and to create a file if it does not exist.
// Input: infile
// Output: none
// In Param: movie, count
// Out Param: none
// Return: none
// Function Called: PrintLine
// Error Messages: none
void LoadFile(Movie* moviePtr[], int& count)
{
ifstream infile;
infile.open("tomatoes.txt");
if(infile.is_open())
{
PrintLine(40, '=');
cout << "LOADING DATA FROM FILE..." <<endl;
while (!infile.eof())
{
moviePtr[count] = new Movie;
getline(infile, moviePtr[count]->nameOfMovie,'|');
infile >> moviePtr[count]->scoreOfMovie;
infile.ignore();
if(!infile.eof())
{
count++;
}
}
}
}
// Function: ReadChoice
// Purpose: To accept an menu choice from the user.
// Input: none
// Output: none
// In Param: choice
// Out Param: choice
// Return: none
// Function Called: PrintLine
// Error Messages: none
void ReadChoice (int& choice)
{
do{
DisplayMenu ();
CheckInput ("Enter 1-4 : ", choice);
PrintLine(40, '=');
}while (choice < ADD || choice > EXIT);
}
// Function: DisplayChoice
// Purpose: To display a menu to the monitor.
// Input: none
// Output: none
// In Param: none
// Out Param: none
// Return: none
// Function Called: PrintLine
// Error Messages: none
void DisplayMenu()
{
PrintLine(40, '=');
cout << "MENU" << endl;
PrintLine(40, '-');
cout << ADD << "." << "Add Movie" << endl
<< PRINT << "." << "Print All" << endl
<< SEARCH << "." << "Search Movie" << endl
<< EXIT << "." << "Exit" << endl;
PrintLine(40, '-');
}
// Function: ProcessChoice
// Purpose: To either add a movie or search the name of a movie based on user input.
// Input: none
// Output: none
// In Param: movie, count, choice
// Out Param: movie, count
// Return: none
// Function Called: AddMovie, SearchMovie
// Error Messages: none
void ProcessChoice (Movie* movie[], int& count, int choice)
{
switch (choice)
{
case ADD:
AddMovie(movie[count], count);
break;
case PRINT:
Print(movie[count], count);
break;
case SEARCH:
SearchMovie(movie[count], count);
break;
}
}
// Function: CheckInput
// Purpose: To stop crashes when program expects numbers but user inputs char or string.
// Input: valid
// Output: none
// In Param: prompt, num
// Out Param: none
// Return: none
// Function Called: none
// Error Messages: Invalid Input
void CheckInput (string prompt, int& num)
{
bool valid;
do{
cout << prompt;
cin >> num;
if (cin.fail())
{
cin.clear();
cout << "Invalid Input!" << endl;
valid = false;
}
else
{
valid = true;
}
cin.ignore(1000,'\n');
} while (!valid);
}
// Function: AddMovie
// Purpose: To add a new movie and give it a rating.
// Input: none
// Output: none
// In Param: movie, count
// Out Param: movie, count
// Return: none
// Function Called: PrintLine
// Error Messages: LIST IS FULL
void AddMovie (Movie movie[], int& count) // made movie an array
{
if(count < MAX_MOVIES)
{
cout << "ADD MOVIE" << endl;
PrintLine(40, '-');
cout << "Title: ";
getline(cin, movie[count].nameOfMovie);
do{
CheckInput("Rating: ",movie[count].scoreOfMovie);
} while (!ScoreValid (movie[count].scoreOfMovie));
}
else
cout << "LIST IS FULL!" << endl;
}
// Function: ScoreValid
// Purpose: To validate the rating assigned by user.
// Input: none
// Output: none
// In Param: score
// Out Param: none
// Return: true or false
// Function Called: none
// Error Messages: none
bool ScoreValid (int score)
{
if (score < 0 || score > 100)
{
cout << "Rating must be between 0 and 100" << endl;
return false;
}
else
return true;
}
// Function: Print
// Purpose: To print out all movies stored in the array
// Input: none
// Output: none
// In Param: moviePtr, count
// Out Param: none
// Return: none
// Function Called: Printline
// Error Messages: none
void Print (Movie const moviePtr[], int count)
{
cout << "Movies:" << endl;
PrintLine(20, '=');
cout << "Title" << setw(16) << "Rating" << endl;
PrintLine(20,'-');
for (int i = 0; i < count; i++)
{
cout << setw(18) << left << moviePtr[i].nameOfMovie << right << moviePtr[i].scoreOfMovie << endl;
}
PrintLine(20,'-');
}
// Function: SearchMovie
// Purpose: To search through an array of previously added movies.
// Input: searchName, found
// Output: none
// In Param: movie, count
// Out Param: movie, count
// Return: true or false
// Function Called: PrintLine, Tomato
// Error Messages: NOT FOUND
void SearchMovie (Movie const movie[], int count)
{
string searchName;
bool found = false;
cout <<"SEARCH MOVIE" << endl;
PrintLine(40, '-');
cout << "Enter Title: ";
getline(cin, searchName);
for(int i = 0; i < count && !found; i++)
{
if (searchName == movie[i].nameOfMovie)
{
cout << setw(30) << left << movie[i].nameOfMovie << right
<< movie[i].scoreOfMovie <<"% " << Tomato(movie[i].scoreOfMovie) << endl;
found = true;
}
}
if (!found)
{
cout << "NOT FOUND" << endl;
}
}
// Function: Tomato
// Purpose: To assign a FRESH or ROTTEN tag to a movie based on rating.
// Input: none
// Output: none
// In Param: score
// Out Param: none
// Return: ROTTEN and FRESH
// Function Called: none
// Error Messages: none
string Tomato (int score)
{
if (score <= 59)
{
return "ROTTEN";
}
else
return "FRESH";
}
// Function: SaveFile
// Purpose: To save any changes to the file
// Input: none
// Output: none
// In Param: score
// Out Param: none
// Return: none
// Function Called: none
// Error Messages: none
void SaveFile( Movie* movie[], int count)
{
ofstream outfile;
outfile.open("tomatoes.txt");
for (int i = 0; i < count; i++)
{
outfile << movie[i]->nameOfMovie <<'|' << movie[i]->scoreOfMovie << endl;
}
cout << "SAVED TO FILE." << endl;
}
// Function: DeletePtr
// Purpose: To free space allocated by the pointer array after the program ends.
// Input: none
// Output: none
// In Param: moviePtr, count
// Out Param: none
// Return: none
// Function Called: none
// Error Messages: none
void DeletePtr(Movie* moviePtr[], int count)
{
for(int i = 0; i < count; i++)
{
delete moviePtr[i];
moviePtr[i] = 0;
}
}
// Function: PrintLine
// Purpose: To format the output
// Input: none
// Output: none
// In Param: size, ch
// Out Param: none
// Return: none
// Function Called: none
// Error Messages: none
void PrintLine (int size, char ch)
{
cout << setw(size) << setfill(ch) << ""
<< setfill(' ') << endl;
}
|