Question: (C++) Update the readRatings function from below to use an array of User objects instead of having a users array and a rating array. readRatings
(C++) Update the readRatings function from below to use an array of User objects instead of having a users array and a rating array.
readRatings function:
int readRatings(string fileName, string users[], int rating[][50], int numUsers, int maxRows, int maxColumns) { if (numUsers == maxRows) { return -2; } int userCount = numUsers; // counter for number of users ifstream ratingsFile; ratingsFile.open(fileName); // open file fileName (ratings.txt) if (ratingsFile.is_open()) // if the input file can be opened { string line = ""; // start with an empty string while (getline(ratingsFile, line) && userCount < maxRows) // while loop for getline as long as the line exists and number of users in array is less than maxRows { string lineSplit[maxColumns + 1]; int splitResult = split(line, ',', lineSplit, maxColumns + 1); // splitting using a comma if (splitResult > 0 ) { users[userCount] = lineSplit[0]; // first index is user for (int i = 0; i < maxColumns; i++) { string ratingItem = lineSplit[i + 1]; if (!ratingItem.empty()) // if it's not empty { rating[userCount][i] = stoi(ratingItem); // string to int } } userCount++; } } } else { return -1; } ratingsFile.close(); // close the file return userCount; // return the number of users in the array }
Please include 3 files - User.h, User.cpp, and readRatingsDriver.cpp which will include the readRatings function and a main function for testing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
