Question
C++ Write a function readRatings that will populate an array of User objects with the name and rating values from the file ratings.txt. Each username
C++
Write a function readRatings that will populate an array of User objects with the name and rating values from the file ratings.txt. Each username represented in ratings.txt is followed by list of integersratings 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! |
This function should:
Accept four arguments in this order:
string: the name of the file to be read
array of User objects: user data
int: number of users currently stored in the array of User object
int: the capacity of the user array [assume a max of 200 users]
Use ifstream, split(), and getline to read and parse data from the file.
For each line in the file
instantiate a User object,
fill in the username data member,
set the size data member equal to the capacity parameter
populate the ratings array with the data in the file, and fill the rest fo the values in the array with the value 1
store a count for the number of ratings that are not 1 in the numRatings
data member, and
append the object to your array of User objects.
Print the username of each user as they are added to the system:
cout << user.getUsername() << "..." << endl
Return the total number of users in the system, as an integer.
If the file cannot be opened, return 1
Important: when testing your readRatings function, make sure it supports multiple calls in a row. For example, you should be able to call the function to read the file ratings1.txt , and then call the function again to read the file ratings2.txt. The result should be an array of User objects, with the users from the first file, followed by the users from the second file.
Expected output:
cynthia...
diane...
joan...
barbara... (etc.)
Here is what I have for the user class - I'm working on getting it to work correctly
header
#pragma once
#ifndef USER_H
#define USER_H
#include
using namespace std;
//Create a class User
class User
{
//Declare two private variables
private:
string username;
int ratings[][200];
int numRatings = 0;
int size = 200;
public:
//Parameterized constructore take in title and author
User(string username, int ratings, int numRatings, int size);
User();
string getUsername() const;
void setUsername(string username);
int getRatingAt();
int setRatingAt(ratings[][200]);
int getNumRatings();
void setNumRatings(int index);
int getSize();
};
#endif // !User_H
cpp
#include "stdafx.h"
#include "User.h"
#include
User::User(string username, int ratings, int numRatings, int size) : username(username), numRatings(numRatings), size(size) {}
User::User() {}
//Get the Username and return it
string User::getUsername() const
{
return username;
}
//set the Username and assign the value of the input string
void User::setUsername(string username)
{
this->username = username;
}
//get the rating
int User::getRatingAt()
{
return ratings;
}
//set the author and assign the value of the input string
void User::setRatingAt(ratings)
{
this->ratings = ratings;
}
//get the num rating
int User::getNumRatings()
{
return numRatings;
}
//set the author and assign the value of the input string
void User::setNumRatings(int numRatings)
{
this->numRatings = numRatings;
}
int User::getSize()
{
return size;
}
int main()
{
User user;
user.setUsername("username1");
User.setRatingingAt(ratings[][200]);
user.setNumRatings(numRatings);
return 0;
}
The Class user specifications
Data members (private): | |
string: username |
|
int array: ratings | Number of elements should be size |
int: numRatings | Number of books in the database |
Int: size | The capacity of the ratings array (200). Constant |
Member functions (public): | |
Default constructor | Sets username to an empty string, numRatings to 0, size to 200, and all the elements of ratings array to the value 1 |
Parameterized constructor | Takes a string, an array of integers, and two integers for initializing username, ratings, numRatings, and size, respectively |
getUsername() | Returns username |
setUsername(string) | (void) Assigns username the value of the input string |
getRatingAt(int) | Parameter: int index. Returns the rating stored at the specified index. If index is larger than the size of the ratings array, returns 1. |
setRatingAt(int,int) | Parameters: int index, int value. Sets the rating to value at the specified index, if index is within the bounds of the array and value is between 0 and 5. Returns a boolean, true if the rating is successfully updated and false otherwise. |
getNumRatings() | Returns numRatings |
setNumRatings(int) | (void) Assigns numRatings the value of the input int |
getSize() | Returns size |
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