Class Program Issue

closed account (92yp4iN6)
Hello. I am writing a program that uses a class to ask a user to input a movie name, MPAA rating, and rate it between 1 and 5 as many times as they choose. I added line 43 'cout<<"Terrible :"<<m1.terrible<<endl;' to test if my counters are adding in the switch statement of 'addRating' function, but it always says the value is 0. How come the counters are not being increased by the rating in my objects? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  #include <iostream>

using namespace std;

class movie {
  public:
    movie () {terrible = 0; bad = 0; ok = 0; good = 0; great = 0;}
    int terrible, bad, ok, good, great; //5 different ratings of the movie

    void setName (string mname); //set function for movie name
    void setRating (string rating); //set function for rating
    string getName () {return name;} //get function to change movie name
    string getRating () {return mpaa;} //get function to change rating

  private:
    string name, mpaa; //name and rating of the movie
};

void addRating (movie m, int num);


int main() {
  string mname, fname, rating, rate;
  int num1, num2, n1, n2, i;

  cout<<"Enter the name of the movie: ";
  cin>>mname;
  cout<<"Enter the MPAA rating of the movie: ";
  cin>>rating;

  movie m1;
  m1.setName (mname);
  m1.setRating (rating);

  cout<<"Enter number of times you would like to rate the movie: ";
  cin>>n1;
  for (i=0; i<n1; i++) {
    cout<<"Enter rating of the movie (1-5): ";
    cin>>num1;
    addRating (m1, num1);
  }

cout<<"Terrible: "<<m1.terrible<<endl;

  cout<<"Enter the name of the movie: ";
  cin>>fname;
  cout<<"Enter the MPAA rating of the movie: ";
  cin>>rate;

  movie m2;
  m2.setName (fname);
  m2.setRating (rate);

  cout<<"Enter number of times you would like to rate the movie: ";
  cin>>n2;
  for (i=0; i<n2; i++) {
    cout<<"Enter rating of the movie (1-5): ";
    cin>>num2;
    addRating (m2, num2);
  }
}

void addRating (movie m, int num) { //must be a number 1-5
  if (num>0 && num<6)
  switch (num) {
    case 1:
      m.terrible++; break;
    case 2:
      m.bad++; break;
    case 3:
      m.ok++; break;
    case 4:
      m.good++; break;
    case 5:
      m.great++; break; }
  else cout<<"Error, number was not 1-5"<<endl;
}

void movie::setName (string mname) {
  name = mname;
  return;
}
void movie::setRating (string rating) {
  mpaa = rating;
  return;
}

How come the counters are not being increased by the rating in my objects?
Because you provide a copy of the movie object. Either you need to provide a reference (movie &m)
or
better yet: make addRating(...) a member function of movie.
Topic archived. No new replies allowed.