Question
C++ Programming, using namespace std and iostream. You were given two text files with comma separated values: books.txt, which is a list of books and
C++ Programming, using namespace std and iostream. You were given two text files with comma separated values: books.txt, which is a list of books and their authors, and ratings.txt, which is a list of users and their ratings of those books. The first task is to read these files and load their contents into arrays for convenient processing. Sample lines from books.txt: Douglas Adams,The Hitchhiker's Guide To The Galaxy Richard Adams,Watership Down Mitch Albom,The Five People You Meet in Heaven (etc.) - The format is
Question 1 Write a function readBooks that populates a pair of arrays with the titles and authors found in books.txt. This function should: Accept five input arguments in this order: string: the name of the file to be read string array: titles string array: authors int: the number of
Question 2 Write a function readRatings that performs a similar task on the user ratings file. Each username represented in ratings.txt is followed by list of integers--ratings of each book in books.txt. Rating Meaning 0 Did not read 1 Hell No - hate it!! 2 Dont like it. 3 Meh - neither hot nor cold 4 Liked it! 5 Mind Blown - Loved it! Your function should: Accept six arguments in this order: string: the name of the file to be read string array: usernames 2D int array: list of ratings for each user (first index specifies user) int : number of users currently stored in the arrays int: row capacity of the 2D array (convention: array[row][column]) [assume to be 100] int: column capacity of the 2D array [assume to be 50] Use ifstream, stringstream, and getline to read and parse data from the file, placing usernames in the usernames array and book ratings in the ratings array (stoi will also be useful here) Print the username of each user as they are added to the system cout << username << "..." << endl; Return the total number of users in the system, as an integer. If the file cannot be opened, return -1 Hint: You can use the split() function that was used during recitation Note: Although user can specify the name of the file to read from, assume the default filename to be ratings.txt Expected output: cynthia... diane... joan... barbara... (etc.)
Question 3 It will be useful to display the contents of your library. Next, make a function printAllBooks that meets the following criteria: Accept three arguments in this order: string array: titles string array: authors int: number of books This function does not return anything If the number of books is 0, print No books are stored Otherwise, print Here is a list of books and then each book in a new line using the following statement cout << titles[i] << " by " << authors[i] << endl; Expected output (assuming you have read the data from books.txt) The Hitchhiker's Guide To The Galaxy by Douglas Adams Watership Down by Richard Adams The Five People You Meet in Heaven by Mitch Albom Speak by Laurie Halse Anderson (etc.)
Question 4 Write a function getUserReadCount for determining how many books a particular user has read and reviewed. This function should: Accept five arguments in this order: string: username for whom we want a read count string array: all users 2D int array: list of all ratings, one row for each user int: number of users in the arrays int: number of books accounted for in the 2D array Return the number of books read/reviewed by the specified user, as an integer. If the program has not read ratings.txt or books.txt, it must read it first before executing this function. In this case, return -1 after printing the following message: cout << name << " does not exist in the database" << endl; If instead the database is initialized but the user is not found, return -1 after printing the following message : cout << name << " does not exist in the database" << endl; Highly recommend: Write a helper function that searches the user array for a particular username and returns its index.
Question 5 Finally, create a function calcAvgRating that returns the average (mean) rating for a particular book. This function should: Accept five arguments in this order: string: book title for which you want the average rating string array: titles 2D int array: list of ratings for each user (same comment here) int: number of users in the arrays int: number of books accounted for in the 2D array Return the average rating of the specified book as a double If the program has not read ratings.txt or books.txt, it must read it first before executing this function. In this case, return -1 after printing the following message: cout << bookTitle << " does not exist in the database" << endl; If instead the database is initialized but the book is not found, return -1 after printing the following message: cout << bookTitle << " does not exist in the database" << endl; Highly recommend: Write a helper function that searches the titles array for a particular book and returns its index. Note: If the user has not reviewed the book it should not be added while calculating the average.
Driver function
The menu will run on a loop, continually offering the user six options until they opt to quit.
Example menu Select a numerical option: ======Main Menu===== 1. Read book file 2. Read user file 3. Print book list 4. Find number of books user rated 5. Get average rating 6. Quit
1. Initialize library Prompt the user for a file name. Pass the file name to your readBooks function. Print the total number of books in the database in the following format: Total books in the database: If no books are saved to the database due to wrong file name, then print the following message: No books saved to the database
2. Initialize user catalog Prompt the user for a file name. Pass the file name to your readRatings function Print the total number of users in the database in the following format: Total users in the database: If no books are saved to the database due to wrong file name, then print the following message: No users saved to the database
3. Display library Call your printAllBooks function.
4. Get number of books reviewed by a user Prompt the user for a username. Pass the username to your getUserReadCount function If the user exists in the system, print the result in the following format: rated books
5. Get average rating for a title Prompt the user for a title. Pass the title to your calcAvgRating function If the title exists in the database, print the result in the following format: The average rating for is Note: is a double with 2 decimal points.
6. Quit Print good bye! before exiting
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started