Home > Enterprise >  Getting huge random numbers in c
Getting huge random numbers in c

Time:06-16

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.

CodePudding user response:

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) {}
  • Related