-1

for my final OOP project i have to create a "Streaming service" in c++, i am basically done with the whole program, i just have one problem. I give a rating to each movie, but when i print this rating, i just get some huge random numbers instead of the actual rating.

here is my movie class and the definition of the function i'm having trouble with.

class movie : public video{
public:
    movie();
    movie(int, std::string, int, std::string, int);

    float getRating();
    void setRating(int);
    void showrating();

protected:
    int rating;

}; 

movie::movie(){
rating = 0;
}

movie::movie(int _id, std::string _name, int _length, std::string 
_genre, int _rating) : video(_id, _name, _length, _genre){}

void movie::showrating(){
std::cout << name <<" has been rated " << rating << " stars out of 5." << std::endl;
}

And here is how i used it in my main.cpp

movie LordOfTheRings(0, "Lord of the Rings", 155, "Adventure", 5);
movie StarWars(1, "Star Wars", 132, "SciFi", 5);
movie Inception(2, "Inception", 143, "Action", 4);
movie Interstellar(3, "Interstellar", 153, "SciFi", 5);
movie Tenet(4, "Tenet", 135, "Action", 3);

movie moviearr [5] = {LordOfTheRings, StarWars, Inception, Interstellar, Tenet};

for (int i = 0; i <= 4; i++){
    moviearr[i].showrating();
}

After running the program, this is the result. The rating should appear there but i'm just getting these random numbers.

Result i'm getting from the code above

I'd really appreciate some help.

  • Maybe you forgot to initialize the rating counter to 0. In either case if you have access to a debugger you should be able to step through your code line by line and look at the value of the rating at each step to determine the reason for the numbers you print. – drescherjm Jun 16 '22 at 03:23
  • 1
    Looks like you did not set `rating` to anything in the constructor. – qrsngky Jun 16 '22 at 03:25
  • 1
    You are not showing the code that contains the problem. – Aganju Jun 16 '22 at 03:31
  • `movie::movie(int _id, std::string _name, int _length, std::string _genre, int _rating) : video(_id, _name, _length, _genre){}` does not use the `_rating` parameter leaving the class member `rating` with no value. – drescherjm Jun 16 '22 at 03:43
  • 1
    A useful tactic is to argue against your compiler. That is, in your question, explain why you believe the compiler messed up. Give us the step-by-step, something like "The definition of `LordOfTheRings` passes `5` as the fifth parameter to the coinstructor. Inside the constructor [...]" and proceed to trace the code path that results in `5` being stored in the `rating` field. It doesn't matter if you think the path is obvious -- write it out. Force yourself to be detailed-oriented and sometimes you'll answer your own question (faster than a Q&A site can). – JaMiT Jun 16 '22 at 04:58
  • Another useful tactic is to [enable warnings when compiling](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – JaMiT Jun 16 '22 at 05:01

1 Answers1

1

You forgot to set rating to anything in your constructor. That's why its value is 'random'

Change this

movie::movie(int _id, std::string _name, int _length, std::string 
_genre, int _rating) : video(_id, _name, _length, _genre){}

to this

movie::movie(int _id, std::string _name, int _length, std::string 
_genre, int _rating) : video(_id, _name, _length, _genre), rating(_rating) {}
john
  • 85,011
  • 4
  • 57
  • 81