Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ only and don't use global variables Write a function readRatings that loads user ratings by reading the ratings.txt file. The first value of each

C++ only and don't use global variables

Write a function readRatings that loads user ratings by reading the ratings.txt file. The first value of each line in ratings.txt is the username. Each username is followed by a list of ratings of the user for 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

    • Array of string: users

    • 2 dimensional int array: ratings - list of ratings for each user.The number of rows corresponds to the number of users, and the number of columns corresponds to the number of books.

    • int: numUsers number of users currently stored in the arrays

    • int: maxRows maximum number of rows of the ratings 2D array (convention: array[row][column]) [assume to be 100]

    • int: maxColumns maximum number of columns of the ratings 2D array [assume to be 50]

  • Use ifstream and getline to read data from the file, placing each username in the users array, and each set of ratings in a row of the ratings 2D array.

  • Hint: You can use the split() - function from problem 3, with comma (,) as the delimiter. When you copy your code in the answer box on Moodle Coderunner, make sure you put both the functions readRatings and split.

  • You can use stoi to convert each rating value (a string, as read from the text file) into an integer value.

  • The function should return the following values depending on cases:

    • Return the total number of users in the system, as an integer.

    • If the file cannot be opened, return -1

    • When numUsers is equal to the maximum number of rows in the ratings 2D array, return -2

    • When numUsers is smaller than the maximum number of rows in the ratings 2D array, keep the existing elements in users array and ratings array, then read data from file and add (append) the data to the arrays. The number of users stored in the arrays cannot exceed the size of the arrays.

    • Empty lines should not be added to the arrays.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions