Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- //struct definitions
- struct movie
- {
- int rank;
- string title;
- int year;
- float boxOffice;
- string rating;
- string director;
- };
- //function prototypes
- int loadMovies(movie[]);
- void printMovie(movie[]);
- void yearRange(movie[], int, int);
- void rating(movie[], string);
- void director(movie[], string);
- void pause();
- int main()
- {
- movie allTime[600];
- int numMovies, menuChoice, year1, year2;
- string rate, dir;
- numMovies = loadMovies(allTime);
- cout << "Loaded " << numMovies << " movies..." << endl;
- do{
- cout << "What information would you like to display? Enter your menu choice." << endl;
- cout << "1. Movies within a year range" << endl
- << "2. Movies of a specific rating" << endl
- << "3. Movies by a particular director" << endl
- << "4. Exit" << endl;
- cin >> menuChoice;
- } while (menuChoice > 5 || menuChoice < 0);
- year1 = 0;
- year2 = 0;
- if (menuChoice == 1)
- yearRange(allTime, year1, year2);
- else if (menuChoice == 2)
- rating(allTime, rate);
- else if (menuChoice == 3)
- director(allTime, dir);
- else if (menuChoice == 4)
- cout << "Goodbye!" << endl;
- pause();
- return 0;
- }
- void yearRange(movie allTime[], int year1, int year2)
- {
- cout << "Enter the year you want to your range to start with: ";
- cin >> year1;
- cout << "Enter the year you want to your range to end with: ";
- cin >> year2;
- for (int i = 0; i < 600; i++)
- {
- if (allTime[i].year >= year1 && allTime[i].year <= year2)
- {
- cout << fixed << setprecision(2);
- cout << right << setw(4) << allTime[i].rank << " "
- << left << setw(30) << allTime[i].title.substr(0, 27)
- << right << setw(5) << allTime[i].year
- << right << setw(8) << allTime[i].boxOffice << " "
- << left << setw(6) << allTime[i].rating
- << left << allTime[i].director << endl;
- }
- }
- }
- void rating(movie allTime[], string rate)
- {
- cout << "Enter the rating: ";
- cin >> rate;
- for (int i = 0; i<600; i++)
- if (allTime[i].rating >= rate)
- {
- cout << fixed << setprecision(2);
- cout << right << setw(4) << allTime[i].rank << " "
- << left << setw(30) << allTime[i].title.substr(0, 27)
- << right << setw(5) << allTime[i].year
- << right << setw(8) << allTime[i].boxOffice << " "
- << left << setw(6) << allTime[i].rating
- << left << allTime[i].director << endl;
- }
- }
- void director(movie allTime[], string dir)
- {
- cout << "Enter the director: ";
- cin >> dir;
- for (int i = 0; i < 600; i++)
- if (allTime[i].director >= dir)
- {
- cout << fixed << setprecision(2);
- cout << right << setw(4) << allTime[i].rank << " "
- << left << setw(30) << allTime[i].title.substr(0, 27)
- << right << setw(5) << allTime[i].year
- << right << setw(8) << allTime[i].boxOffice << " "
- << left << setw(6) << allTime[i].rating
- << left << allTime[i].director << endl;
- }
- }
- int loadMovies(movie allTime[])
- {
- //load all movies from file
- int count = 0;
- ifstream inFile;
- string junk;
- //open file
- inFile.open("movies.txt");
- if (!inFile)
- cout << "File did not open. ";
- else
- {
- cout << "File opened successfully. ";
- inFile >> allTime[count].rank; //will read up until .00
- while (!inFile.eof())
- {
- inFile.ignore(4); //clear the .00|
- getline(inFile, allTime[count].title, '|');
- inFile >> allTime[count].year; //will read up to .00
- inFile.ignore(5); //clear the .00|$
- inFile >> allTime[count].boxOffice; //will read till |
- inFile.ignore(); //ignore |
- getline(inFile, allTime[count].rating, '|');
- //skip next 3 fields
- getline(inFile, junk, '|');
- getline(inFile, junk, '|');
- getline(inFile, junk, '|');
- getline(inFile, allTime[count].director, '|');
- getline(inFile, junk); //junk rest of line until enter
- count++;
- inFile >> allTime[count].rank; //will read up until .00
- }
- inFile.close();
- }
- return count;
- }
- void pause()
- {
- cout << "Press any key to continue....";
- while(1)
- {
- if(kbhit())
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement