Question

C++ I only need a clear explanation on how to read the file, then get the ratings from a txt file as integers by splitting by comma. (The first item on each line is always the username.) Sum up everything that is NOT 0 and divide by the total amount not including 0 ratings.

No vectors

using namespace std

The member function getCountReadBooks which determines how many books a particular user has read and reviewed. This function should:
● Accept one argument:
○ string: username
● The username and book title search should be case insensitive. For example,
“Ben, “ben” and “BEN” are one and the same user.
● The function should return the following values depending on cases:
○ Return the number of books read/reviewed by the specified user if user is found
○ Return -3 if the username is not found

I get when the username is not found. But how do I approach it when the user has reviewed a book? Especially when it is a 0 rating in a rating.txt file.

rating.txt:

User1,1,4,2

User2,4,2,0

User3,0,0,3

Here is my code so far:

bool stringComp(const string& str1, const string& str2) {
if (str1.size() != str2.size()) {
return false;
}
for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c1 != str1.end(); ++c1, ++c2) {
if (tolower(*c1) != tolower(*c2)) {
return false;
}
}
return true;
}

getCountReadBooks(string userName) {
int userIndex = -1; //find username
for(int i=0; i if(stringComp(userName, users[i])) {
userIndex = i;
}
}
int titleIndex = -1; //find title
for(int i=0; i if(stringComp(bookTitle, books[i])) {
titleIndex = i;
}
}
if(userIndex == -1) { //if no user return -3
return -3;
}
//return number of books rated
}

I am a beginner so please provide simple explanations/code

Edit:

I hope this photo will help. To sum it up this question is part of a large class and can use other functions within the functions. But there is too much code to paste here so all I can do is post this.

Library Book books[ ] User users sizeU ser sizeBook numBooks numUsers Books I The Hitehhikers Guide to The title Myth rate

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since the entire code is very large and you cannot paste it here for obvious reasons I am gonna guide you to complete this assignment.

getCountReadBooks(string userName) {
int userIndex = -1; //find username
for(int i=0; i if(stringComp(userName, users[i])) {
userIndex = i;
}
}
int titleIndex = -1; //find title
for(int i=0; i if(stringComp(bookTitle, books[i])) {
titleIndex = i;
}
}
if(userIndex == -1) { //if no user return -3
return -3;
}
//return number of books rated
}

Since you have mentioned no vectors, you need you use arrays to get the ratings from rating.txt. But for vector you just need to do this: v.push_back(d); // add rating to vector

***************************************************************************************************************************************

But withot vector we need an array. You need to put the string with name and ratings into the string line like below. this will calculate when it is a 0 rating in a rating.txt file

//..

..

Code for reding file and getting the line

..

//

string line = "User1,1,2,1,3,0";
stringstream str(line);
string temp;

int size = 0;
// Tokenizing by ','
while(getline(str, temp, ',')) {
size++;
}
size--;
int ratings[size-1];//First element is the Name
stringstream str1(line);
for(int i = 0; getline(str1, temp, ','); i++){
if(i == 0)
continue;
else{
int rating;
std::istringstream iss (temp);
iss >> rating;
ratings[i-1] = rating;//Puting ratings in the array
}
}

int nonZeroRatings = 0;//Total number of non zero ratings
int nonZeroRatingSum = 0;//Total sum of non zero ratings
for(int i = 0; i<size; i++){
if(ratings[i]!=0){
nonZeroRatings++;

   //This line sums up everything that is NOT 0
nonZeroRatingSum+=ratings[i];//Total
}
}

if(nonZeroRatings !=0){

//this line divides the rating by the total amount not including 0 ratings
double average = (double)nonZeroRatingSum/nonZeroRatings;//Average
cout << "Avg = " << average << endl;

//return average;

}else{

cout << "No ratings available "<< endl;

//return 0;

}

*********************************************************************************************************************

The line marked in bold is the line you need to use in you function to return the non zero ratings

Also read the commented line in bold for better understanding

Add a comment
Know the answer?
Add Answer to:
C++ The member function getCountReadBooks which determines how many books a particular user has r...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT