Question: Objective: Learn how to define and use structures, create and access a vector of structures, use sort with different compare functions. Assignment: You will define

Objective: Learn how to define and use structures, create and access a vector of structures, use sort with different compare functions.

Assignment: You will define a structure to receive the title, author and genre. You can use the same method that you learned to strip the book title to get the other parts. Instead of just a vector of strings (title), you will now need a vector of structures. Each element in the vector will have the book's title, author and genre. Your program needs to be able to sort the vector by title, author or genre, and print the books from a specific genre. For example, I must be able to ask your program to print all the fiction books.

Detailed specifications:

Define a structure with fields to store each part of the book information: title, author and genre. The structure can be defined in the global scope as it is not a variable. A structure is a type.

Declare a variable, whose type is the structure, to receive the book info, and also a vector of structures that will store all the books. Remember that variables should not be global.

Read the each line of the file the same way you did before, making it a string stream, but now you will need to call getline on the stringstream repeatedly to get each part following the title. After populating the structure variable with the book info, push it into the vector.

Design an interface to let the user of your program choose which field they want to see the books sorted by (title, author or genre). Therefore you will need to define three different compare functions to pass to sort. Here are examples of how you would call sort with different compare functions:

sort (books.begin(), books.end(), compareByTitle); sort (books.begin(), books.end(), compareByAuthor); sort (books.begin(), books.end(), compareByGenre); sort will use the function that you provide to sort the data. This is how function compareByTitle should look like, assuming Book is the name of your structure: bool compareByTitle(Book book1, Book book2) { return book1.title < book2.title) } Display the books sorted by the field that the user chooses. You should display ALL fields no matter how the data is sorted. I expect that you split your program in several functions, instead of having one big main function. Define separate functions to:

Read the data from the file and populate the vector. Notice that vectors are not arrays and need to be passed by reference.

Have an interface with the user asking which fields should be used for sorting and call sort with the appropriate compare function.

Print the book info. Vectors should be passed by reference to avoid unnecessary copying. Pass it as a const reference since it does not need to be changed inside this function.

Input: input is in txt file named "Books.txt"

Books.txt Starting out with c++, Tony Gaddis, technical Fundamentals of Database Systems, Elmarsi & Navathe, technical The C++ Programming Language, Bjarne Stroustrup, technical The Pillars of the Earth, Ken Follett, historical fictionFall of Giants, Ken Follet, historical fiction Mindset: The New Psychology of Success, Carol Dweck, psychology One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction Ashes, Kenzo Kitakana, fiction The Dark Forest, Liu Cixin, science fiction Replay, Ken Grimwood, fantasy

Output: This is how the output should look like if the user chooses to sort by. Here is an example of an output if the user chooses to sort by genre: Replay, Ken Grimwood, fantasy One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction Ashes, Kenzo Kitakana, fiction The Dark Forest, Liu Cixin, science fiction The Pillars of the Earth, Ken Follett, historical fiction Fall of Giants, Ken Follet, historical fiction Mindset: The New Psychology of Success, Carol Dweck, psychology Starting out with c++, Tony Gaddis, technical Fundamentals of Database Systems, Elmarsi & Navathe, technical The C++ Programming Language, Bjarne Stroustrup, technical Take a screenshot of the output your program generates and upload it with your source code.

Documemtation Guidline Above function headers Write a comment above each function header explaining what the function does and the purpose of each parameter and return type. Example: /* This function adds the amount passed as parameter to the balance in the account and returns whether the operation was successful. amount: the amount to be deposited Return value: true means the deposit was successful, false means the deposit was unsuccessful */ bool deposit (double amount) { ... } Next to variable declarations Write a comment next to variables that are not self-explanatory. For example, a variable named interestRate doesn't need an explanation if you are using it to store interest rate, but if you have a variable named x, you need to explain what you use it for. Comments are supposed to add meaning to your code. The following are examples of comments that do NOT have any purpose in a program because they are too obvious, and therefore must be avoided: // variable declarations int x; // integer variable void printReport(); // function prototype

Please attach a screenshot of the output file and notice that the input file is in a txt file as written above! Thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!