Objective
To gain experience with using an array of structures, as well as with dynamic allocation of an array. This program provides further practice in file input, strings (either c-strings or string objects, programmer's choice), function building, if statements/switch, loops, and general code task problem solving and layout.
Filename:movies.cpp
Task
Seeing our lifestyle has changed a bit lately, you've suddenly found yourself to have so much time to watch those movies you have on your list. The problem? Your movie list is so long! Let's create a movie data organizer to help us organize our movie collection. This assignment will simulate the loading, manipulation, and exporting of a movie "database" which will be a collection of movie information that comes from a .txt file.
Input File Format
- The first line of the input file will be a single integer number -- this value tells how many movies are in this movie database text file. There's no guarantee how many records will be in a file that the user opens, so you must create your dynamic array of structs only AFTER reading in this first integer value.
- After this first line, each line following represents one movie entry with data in the following format on each line:
GENRE:MOVIE TITLE, year runtime rating
More specifically, each of the movie data lines contain:
the Genre of the Movie (can be ACTION, DRAMA, SCIENCE FICTION, or COMEDY) a colon : the full movie title a comma a space the year a space the runtime in minutes a space the rating (can be G, PG, PG-13, R) a newline
- You may assume that any given movie files are in the correct format, and that any files we test with your program will also be of the same, above, correct format. You can also assume there there is no trailing whitespace in the file, and that the types and limits will be as described above. Finally, you can assume the file will at least contain one movie (there will be no files we use to test your program that contain zero movies)
You SHOULD test on many different files that have a different number of records in the file.
DO NOT ever open your input file in "notepad" (windows utility). This will cause your file to have different newline chars used, and cause issues while trying to read in the file. Make sure to test your program with a few different movie text files. If your assignment only works with one sample movie file/one size, you'll lose 50 points.
Sample movie data input files
Sample input file 1.
Sample input file 2.You can right click that link, "save as" , and save it to your machine. Then, upload it to linprog. Avoid opening this file in any other text editor besides our unix text editors as some editors can mess with the file's layout.Program Details
Your program's overall goal is to allow the user to select menu options that will ultimately load the data in a movie file (a movie "database") in to your program, and then work with that database. We are going to store our movie database in an ARRAY of STRUCTURES. Specifically, an array of structures of type Movie (an array of movies! :) ).
- Start withTHIS STARTER FILE. I've helped to lay some things out for you that will get you started. I've also declared the pointer of type Movies named movieArr, and initialized it to the Null Pointer for you. The pointer being set to NULL can specify that no array has been allocated yet to store a file's contents. You'll use this pointer anytime you need to allocate a new dynamic array to then store some movie data from a file.
- Create a structure type calledMovies. Each object of typeMoviesshould contain fields for the following:
- genre (a string or c-string)
- movie title (a string or c-string)
- movie rating (a string or c-string)
- Release year of the movie (an integer)
- Runtime of the movie (in minutes) (an integer)
- Enter a menu loop that prints the following menu and allows the user to choose from the following options (they should enter the corresponding value to choose):
1 - Load Movie File 2 - View Total Movie Database Runtime 3 - View Movies by Rating 4 - Export Movies by Genre 5 - Clear Currently Loaded Database 6 - Print Database Sorted by Year Q - Quit Selection >
Should you decide to add additional features, you're welcome to add options to this menu. If you choose to not do option 6 (which is for extra credit), you may omit it from your menu. Note if the user enters an invalid menu option, you should handle this and allow the user to keep trying to enter valid options.
You can print this menu each time the user makes a selection, or just once at the beginning of your program (you can add a "view menu again" option to the menu if you want!). Note that since the user can eiter digits or letters as a part of their menu entry, you'll need to consider the best type of variable to read these menu choices in to.
MENU OPTION 1: LOAD MOVIE FILE
When this option is selected, you should attempt to load a new file of movie records into your array named movieArr.
Your program should start by asking the user to type the name of the movie input file. Whenever a bad filename is entered, print an error message and prompt the user to re-enter the filename. The user should be ***forced*** to enter a correct filename. If the user enters an incorrect file name, ask them to enter again, over and over, until they enter a valid file. Check our course notes for this... there's a section called "user entered file names" that will be very helpful here. You may assume that the user entered filename will not be longer than 40 characters.
Once the file is open and ready to read, read in the integer at the top of the movie file FIRST, and INITIALIZE your DYNAMIC array of type Movies (movieArr) to be of the size corresponding to the integer you just read in. Set your Movies pointer moviesArr equal to this to this newly created array, and then begin reading in the rest of the contents of the movie file into the appropriate sections of your Movies array. Don't forget to close the input file when you're done reading from it.
Note that if this option is selected, and a movie database is already loaded, ask the user for a yes or no (y/n) response on if they want to proceed before deleting the old information and loading in new information:
There is a database already loaded. This will overwrite the current database. Continue? (y/n) >
Make sure the user enters a valid character here (y or n).
MENU OPTION 2: VIEW TOTAL MOVIE DATABASE RUNTIME
This option when selected should print the total movie database runtime in hours and minutes. For instance, if my movies currently stored in my array were 674 minutes long combined, I would print:
runtime: 11hrs 14mins
If the movie array is empty and not currently loaded with information when this option is selected, it should print:
runtime: 0hrs 0mins
MENU OPTION 3: VIEW MOVIES BY RATING
When the user selects this option, ask the user for a movie rating. They can enter any value here, but the movie ratings we can expect in our movie files are G PG PG-13 and R. Once the user selects a movie rating, print a listing of all the movies in your currently stored array that are of that exact rating. (You do not need to account for lowercase/uppercase here, exact match is good). See sample runs for what this list should look like.
If this option is selected when the movie array database is empty/not populated, print the message:
Load movie file first! Use option 1.
MENU OPTION 4: EXPORT MOVIES BY GENRE
Upon the selection of this option, the goal is to create 4 text files named:
action.txt drama.txt comedy.txt scifi.txt
After you create these files, go through your movie array and print out the movie title and year to the appropriate file that matches the genre of the movie in your movie array database.
After this menu selection, movies of the appropriate genre should be printed to the appropriate files in this format:
Die Hard (1988) The Matrix (1999) Ghostbusters (1984)
Don't forget to CLOSE the files when done! If these files already exist, it is OK to overwrite their contents.
If this option is selected when the movie array database is empty/not populated, print the message:
Load movie file first! Use option 1.
MENU OPTION 5: CLEAR CURRENTLY LOADED MOVIE DATABASE
When the user selects this option, the goal here is to wipe out, or essentially "delete" our previous movie array. You can set your movieArr pointer to NULL if you like whenever it is not in use. Your movieArr pointer will now be free to create and utilize another array at a later time in the program.
If this option is selected when the movie array is ALREADY empty, print the message:
Movie database already empty.
MENU OPTION 6 (EXTRA CREDIT): SORT MOVIES BY YEAR = 10 BONUS POINTS
Upon the selection of this option, you should sort the movies currently loaded into your movieArr by the release year. (Don't mind if you do this oldest --> newest, or opposite).
To earn these 10 points, write a portion of code that sorts your movie array by year, and then use the following loop to print out the contents of your array.
for(int i = 0; i
You can change the variable names if mine above don't match yours, but you must use a loop from index 0 to the end of the array, printing out your movies, which should now be in sorted order.
If this option is selected when the movie array database is empty/not populated, print the message:
Load movie file first! Use option 1.
Other hints and to-knows:
Make sure to clean up any remaining dynamically allocated space before ending the programMake sure to close any input files before ending the programIf you choose to use c-strings, you can assume a max size of 20 for the genre, 100 for the title, and 10 for the rating.There are no "easter egg" bonuses on this assignment. The only bonus opportunity is Menu Option 6.You need to "error check": the user's menu choice, the user's y/n entry on loading a movie file when one is loaded already, and also on the filename that the user specifies that they'd like to open. You may assume the user complies everywhere else.the .ignore() function may be handy to you:
cin.ignore(100,'\n'); //this ignores up to 100 or up to \n on the KEYBOARD cin.ignore(); //ignores the next 1 character entered by the user cin.ignore(10); // ignores the next 10 characters entered by the user Replace "cin" with the name of an ifstream object to instead ignore characters from within the file, if you find that helps.
Functions
You'll notice that there were no specified functions that your program must have. It is YOUR JOB as the PROGRAMMER to come up with useful functions for your code.This will be a graded factor of your program, how well you utilize and build functions for yourself
Program Requirements
- No global variables, other than constants
- You may use any of these libraries:
- iostream
- iomanip
- fstream
- cctype
- cstdlib
- cstring
- string
- HINT: Be careful if you change the example input file. If you have trailing whitespace at the end of a text file your program attempts to read you could encounter off by one errors. You SHOULD test your program on multiple input files (all of the required format of course)
- Write your source code so that it is readable and well-documented
- Part of assessing readability and style will be how well you break your program into appropriatefunctions.