Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. //struct definitions
  9. struct movie
  10. {
  11.     int rank;
  12.     string title;
  13.     int year;
  14.     float boxOffice;
  15.     string rating;
  16.     string director;
  17. };
  18.  
  19.  
  20. //function prototypes
  21. int loadMovies(movie[]);
  22. void printMovie(movie[]);
  23. void yearRange(movie[], int, int);
  24. void rating(movie[], string);
  25. void director(movie[], string);
  26. void pause();
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32.     movie allTime[600];
  33.     int numMovies, menuChoice, year1, year2;
  34.     string rate, dir;
  35.    
  36.    
  37.     numMovies = loadMovies(allTime);
  38.     cout << "Loaded " << numMovies << " movies..." << endl;
  39.    
  40.     do{
  41.         cout << "What information would you like to display? Enter your menu choice." << endl;
  42.         cout << "1. Movies within a year range" << endl
  43.         << "2. Movies of a specific rating" << endl
  44.         << "3. Movies by a particular director" << endl
  45.         << "4. Exit" << endl;
  46.         cin >> menuChoice;
  47.        
  48.     } while (menuChoice > 5 || menuChoice < 0);
  49.    
  50.     year1 = 0;
  51.     year2 = 0;
  52.     if (menuChoice == 1)
  53.         yearRange(allTime, year1, year2);
  54.     else if (menuChoice == 2)
  55.         rating(allTime, rate);
  56.     else if (menuChoice == 3)
  57.         director(allTime, dir);
  58.     else if (menuChoice == 4)
  59.         cout << "Goodbye!" << endl;
  60.    pause();
  61.    
  62.    
  63.     return 0;
  64.    
  65. }
  66.  
  67.  
  68. void yearRange(movie allTime[], int year1, int year2)
  69. {
  70.     cout << "Enter the year you want to your range to start with: ";
  71.     cin >> year1;
  72.     cout << "Enter the year you want to your range to end with: ";
  73.     cin >> year2;
  74.    
  75.     for (int i = 0; i < 600; i++)
  76.     {
  77.         if (allTime[i].year >= year1 && allTime[i].year <= year2)
  78.         {
  79.             cout << fixed << setprecision(2);
  80.             cout << right << setw(4) << allTime[i].rank << " "
  81.             << left << setw(30) << allTime[i].title.substr(0, 27)
  82.             << right << setw(5) << allTime[i].year
  83.             << right << setw(8) << allTime[i].boxOffice << " "
  84.             << left << setw(6) << allTime[i].rating
  85.             << left << allTime[i].director << endl;
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. void rating(movie allTime[], string rate)
  92. {
  93.     cout << "Enter the rating: ";
  94.     cin >> rate;
  95.    
  96.     for (int i = 0; i<600; i++)
  97.         if (allTime[i].rating >= rate)
  98.         {
  99.             cout << fixed << setprecision(2);
  100.             cout << right << setw(4) << allTime[i].rank << " "
  101.             << left << setw(30) << allTime[i].title.substr(0, 27)
  102.             << right << setw(5) << allTime[i].year
  103.             << right << setw(8) << allTime[i].boxOffice << " "
  104.             << left << setw(6) << allTime[i].rating
  105.             << left << allTime[i].director << endl;
  106.         }
  107.    
  108.    
  109.    
  110. }
  111.  
  112.  
  113. void director(movie allTime[], string dir)
  114. {
  115.     cout << "Enter the director: ";
  116.     cin >> dir;
  117.    
  118.     for (int i = 0; i < 600; i++)
  119.         if (allTime[i].director >= dir)
  120.         {
  121.             cout << fixed << setprecision(2);
  122.             cout << right << setw(4) << allTime[i].rank << " "
  123.             << left << setw(30) << allTime[i].title.substr(0, 27)
  124.             << right << setw(5) << allTime[i].year
  125.             << right << setw(8) << allTime[i].boxOffice << " "
  126.             << left << setw(6) << allTime[i].rating
  127.             << left << allTime[i].director << endl;
  128.         }
  129.    
  130.    
  131.    
  132. }
  133.  
  134. int loadMovies(movie allTime[])
  135. {
  136.     //load all movies from file
  137.     int count = 0;
  138.     ifstream inFile;
  139.     string junk;
  140.    
  141.     //open file
  142.     inFile.open("movies.txt");
  143.    
  144.     if (!inFile)
  145.         cout << "File did not open. ";
  146.     else
  147.     {
  148.         cout << "File opened successfully. ";
  149.        
  150.         inFile >> allTime[count].rank;                  //will read up until .00
  151.        
  152.         while (!inFile.eof())
  153.         {
  154.             inFile.ignore(4);                               //clear the .00|
  155.            
  156.             getline(inFile, allTime[count].title, '|');
  157.            
  158.             inFile >> allTime[count].year;                  //will read up to .00
  159.             inFile.ignore(5);                                       //clear the .00|$
  160.            
  161.             inFile >> allTime[count].boxOffice;             //will read till |
  162.             inFile.ignore();                                        //ignore |
  163.            
  164.             getline(inFile, allTime[count].rating, '|');
  165.            
  166.             //skip next 3 fields
  167.             getline(inFile, junk, '|');
  168.             getline(inFile, junk, '|');
  169.             getline(inFile, junk, '|');
  170.            
  171.             getline(inFile, allTime[count].director, '|');
  172.            
  173.             getline(inFile, junk);                  //junk rest of line until enter
  174.            
  175.            
  176.             count++;
  177.             inFile >> allTime[count].rank;          //will read up until .00
  178.            
  179.         }
  180.        
  181.        
  182.         inFile.close();
  183.     }
  184.    
  185.    
  186.     return count;
  187. }
  188.  
  189.  
  190. void pause()
  191. {
  192.     cout << "Press any key to continue....";
  193.     while(1)
  194.     {
  195.         if(kbhit())
  196.         {
  197.             break;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement