#Why am I getting this error?

13 messages · Page 1 of 1 (latest)

pliant ivy
#

Hello, I am doing a homework assignment for my C++ course at university. For this assignment, I have a text file of movie ratings. For each movie, the first line is the number of ratings, the second is the title of the movie and the third is the average rating out of 5 stars.

An example of one of the movie ratings in the text file:
10
Harry Potter and the Order of the Phoenix
4

For this assignment, I have to use std::map to store all of the reviews, where the key is the movie title and it contains a pair with the number of reviews and the average review. I also must incorporate an iterator into the solution.

Here is file with the main function:

#include <iostream>
#include <fstream>
#include "MovieCritic.h"

int main()
{
std::ifstream file("movies.txt");

if (!file.is_open())
{
    std::cerr << "Error opening the file!";
    return 1;
}

MovieCritic list;
file >> list;

file.close();

std::cout << "Movie Ratings: " << std::endl;
std::cout << list;

return 0;

}

spare heraldBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pliant ivy
#

When I run the code, nothing prints and I get this message and I want to know how to fix it.

chrome hearth
#

!f

spare heraldBOT
#

Here is the header file:
#ifndef MOVIECRITIC_H
#define MOVIECRITIC_H

#include <string>
#include <map>
#include <iostream>
#include <fstream>

using std::map;
using std::string;
using std::pair;

class MovieCritic {
 private:
  map<string, pair<int, int>>
      ratings;  // movie title, number of reviews, average review

 public:
  MovieCritic() : ratings() {}  // default constructor initializing an empty map

  friend std::ifstream& operator>>(
      std::ifstream& in,
      MovieCritic& r);  // takes in a whole file of ratings
  friend std::ostream& operator<<(
      std::ostream& out,
      const MovieCritic& r);  // outputs the map of ratings
};

#endif // !MOVIECRITIC_H

Arcana
chrome hearth
#

!f

spare heraldBOT
#

Here is the implementation file:
#include "MovieCritic.h"

std::ifstream& operator>>(std::ifstream& file, MovieCritic& r)
{
std::string line;
```cpp
int numRatings;
int averageRating;
std::string title;

while (std::getline(file, line)) {
numRatings = std::stoi(line);
std::getline(file, line); // Read the next line for averageRating
averageRating = std::stoi(line);
std::getline(file, title); // Read the next line for title
r.ratings[title] = std::make_pair(numRatings, averageRating);
}

return file;
}

std::ostream& operator<<(std::ostream& out, const MovieCritic& r) {
for (auto it = r.ratings.begin(); it != r.ratings.end();
++it) // traverse through the map and print out the values
{
out << it->first << ": " << it->second.first << " reviews, average of "
<< it->second.second << "/5" << std::endl;
}
return out;
}

Arcana
pliant ivy
#

The error didn't send before, so I'm sending it again

chrome hearth
#

can't see it

#

i see it now

mental dust
#

Press retry and then look at the call stack

#

(or just start your program via the debugger)