Question: You will write a program to read a file containing a list of books (tile, author, genre, year published) into a vector of structures. Use
You will write a program to read a file containing a list of books (tile, author, genre, year published) into a vector of structures. Use the book.cpp file provided to help you. Please note that it is ok to define the structure in the global scope only because it is the definition of a type. Do not declare any global variables. All variables should be local.
After the program reads the data from the file, have your program give the user options to see the movies on the screen sorted by: title, author, genre or year published.
Use the sort function (#include
sort (myMovies.begin(), myMovies.end(), compareByTitle);
sort will use the function that you provide to sort the data. You will need four different compare functions, because there are four different fields. If the user asks to see the information sorted by title, you need to call sort passing compareByTitle. If he/she asks for the books sorted by author, you will need to call sort passing compareByAuthor, and so forth. Define one compare function for each field.
This is how function compareByTitle should look like:
bool compareByTitle(Book b1, Book b2) { return b1.title < b2.title) }
It will sort the vector by comparing the titles of each two elements in the vector.
After sorting, you should display all the information of each book in a table format. No matter how the data is sorted, you should always print all the fields. Define a function that prints the whole vector. The print function should print a header like the following and then all the data that is in the vector sorted by the required field. Use a range-based for loop.
This is how the information should be presented on the screen:
Title Author Genre Year Published
---------------------------------------------------------------------------
The general structure of your program after calling the getBooks function should look like the following:
repeat {
display menu of options
sort the data according to option chosen by user
print sorted vector
} until the user wants to quit.
Have fun!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
