Changing class stats in another function?

So I'm working on code to allow for multiple pets to be played with...I've gotten almost to where I need to be, but I still have two more problems. I need a random event to happen to a random pet and affect their statistics, and I need to have the "exit" option actually exit the program. I'm stuck on both of these, and I was hoping to get advice.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  /*

03/20/2018
Section 02
Assignment 07
*/

#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;

// declaration of the pet class
class pet {
private:
	int hunger;           // private data member
	string species;       // private data member
	int happy;            // private data member
	int sleepy;           // private data member
public:
	pet();                // constructor
	void play();          // public member function
	void feed();          // public member function
	void nap();           // public member function
	void print();         // public member function
	void randomevent();
	int check_health();   // public member function
	string name;
};

pet petArray[4];
int petchoice;
int choice;
int health_check;

int main()
{
	petArray[0].name = "Shadow";
	petArray[1].name = "Freddie";
	petArray[2].name = "Moon Moon";
	petArray[3].name = "Sadie";

	do {
		cout << "\nWhich pet would you like to play with?\n";
		cout << "\n0 - Shadow\n1 - Freddie\n2 - Moon Moon\n3 - Sadie\n4 - Exit\n\n";
		cin >> petchoice;
		cout << endl;
		petArray[petchoice].print();
		cout << endl;
		cout << "What would you like to do with your pet?\n";
		cout << "Play (1) \nFeed (2) \nNap (3)\n";
		cin >> choice;
		switch (choice)
		{
		case 1:
			petArray[petchoice].play();
			break;
		case 2:
			petArray[petchoice].feed();
			break;
		case 3:
			petArray[petchoice].nap();
			break;
		}
		petArray[petchoice].print();
		health_check = petArray[0].check_health();
		petArray[0].randomevent();
	} while (choice != 0 && (!petArray[0].check_health() || !petArray[1].check_health() || !petArray[2].check_health() || !petArray[3].check_health()));
	cin.ignore();
	cout << "Press enter to exit." << endl;
	cin.ignore();
	return 0;
}

/* Constructor, creates a new pet with starting values. */
pet::pet() {

	hunger = 50;
	happy = 50;
	sleepy = 50;
}
/* Member function play(), allows playing with a pet. */
void pet::play() {
	int choice = 0;
	cout << "What should we play?\n";
	cout << "Fetch (1) \nRoll over (2) \n";
	cin >> choice;
	switch (choice) {
	case(1):
		cout << "\nGo get the ball! Fetch!\n";
		happy += 10;
		hunger += 5;
		break;
	case(2):
		cout << "\nCome on, roll over!\n";
		happy += 5;
		hunger += 1;
		break;
	default:
		cout << "Not a valid choice." << endl;
	}
}

/* Member function feed(), allows the user to feed a pet. */
void pet::feed()
{
	int choice = 0;
	cout << "What do you want to feed your pet?\n";
	cout << "1 - Health Food\n2 - Treat\n";
	cin >> choice;
	switch (choice)
	{
	case(1):
		cout << "I feel great!\n";
		happy -= 1;
		hunger -= 10;
		break;
	case(2):
		cout << "\nMMM, Yummy!\n";
		happy += 5;
		hunger -= 3;
		break;
	default:
		cout << "Invalid choice." << endl;
	}
}

/* Member function nap(), allows the user to have their pet take a nap. */
void pet::nap()
{
	cout << "Your pet is taking a nap! So relaxing!\n";
	hunger += 10;
	sleepy -= 20;
	happy += 5;
}

/* Member function print(), prints information about a pet. */
void pet::print() {
	cout << "\nYour pet " << name << " is \n";
	cout << "Happy: " << happy << endl;
	cout << "Hungry: " << hunger << endl;
	cout << "Sleepy: " << sleepy << endl;
}

/* Member function check_health(), checks the health of a pet. */
int pet::check_health() {
	if (hunger >= 100) {
		cout << "\nYour pet has starved.\n";
		return 1;
	}
	if (happy <= 0) {
		cout << "\nYour pet has died of a broken heart.\n";
		return 1;
	}
	if (sleepy >= 100) {
		cout << "\nYour pet has perished of exhaustion.\n";
		return 1;
	}
	return 0;
}

void pet::randomevent()
{
	int petRandom = rand() % 4;
	int choice = rand() % 4;
	switch (choice)
	{
	case 0:
		cout << "\n" << petArray[petRandom].name << " was sprayed by a skunk!\n";
		happy -= 20;
		break;
	case 1:
		cout << "\n" << petArray[petRandom].name << " is happy to be with you!\n";
		happy += 10;
		break;
	case 2:
		cout << "\n" << petArray[petRandom].name << " ate some human food and threw up!\n";
		happy += 5;
		hunger += 30;
		break;
	case 3:
		cout << "\n" << petArray[petRandom].name << " was barking all night!\n";
		sleepy += 20;
		break;
	}
}
but I still have two more problems

Optimist! :-)

I’ve added comments to your code, but first:
- I think you’d better keep your classes declarations and definitions closer to each other.
You use the randomevent() member as if it were a separate function, asking it to choose which class instance apply the changes to. It seems you missed the point of being a member function.

- I’m afraid you abused global variables: you could move all of them inside main().

- Meaningless comments worsen the code. What does “private data member” clarify?

- Magic numbers are insidious. What about substituting a const expression for them?
Es. constexpr int PETS_NUMBER = 4;

- Before using rand() you usually want to seed srand():
http://en.cppreference.com/w/cpp/numeric/random/srand
srand() is to be initialised only once.

Your code + some comments (starting with “-->”):
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
   03/20/2018
   Section 02
   Assignment 07
*/

#include<iostream>
#include<string>
#include<stdlib.h> // --> C header. Use the C++ corresponding
#include<time.h> // --> C header. Use the C++ corresponding

using namespace std; // --> C'mon! Just type std:: a few times and avoid this.

// declaration of the pet class
class pet {
private:
    int hunger;           // private data member
    string species;       // private data member
    int happy;            // private data member
    int sleepy;           // private data member
public:
    pet();                // constructor
    void play();          // public member function
    void feed();          // public member function
    void nap();           // public member function
    void print();         // public member function
    void randomevent();
    int check_health();   // public member function
    string name;
};

// --> Pointless global variables: move them inside main()
pet petArray[4];    // --> magic numbers often introduce hard to find bugs
int petchoice;
int choice;
int health_check;

int main()
{
    petArray[0].name = "Shadow";
    petArray[1].name = "Freddie";
    petArray[2].name = "Moon Moon";
    petArray[3].name = "Sadie";

    do {
        cout << "\nWhich pet would you like to play with?\n";
        cout << "\n0 - Shadow\n1 - Freddie\n2 - Moon Moon\n3 - Sadie\n4 - Exit\n\n";
        // --> What if I answer -1 or 5?
        cin >> petchoice;
        cout << endl;
        // --> QUESTION: I need to have the "exit" option actually exit the program
        // --> Hints: what does it happen if you return from main()?
        // -->        what does it happen if you break inside a loop?
        petArray[petchoice].print();
        cout << endl;
        cout << "What would you like to do with your pet?\n";
        cout << "Play (1) \nFeed (2) \nNap (3)\n";
        // --> What if I answer 0 or 4?
        cin >> choice;
        switch (choice) {
        case 1:
            petArray[petchoice].play();
            break;
        case 2:
            petArray[petchoice].feed();
            break;
        case 3:
            petArray[petchoice].nap();
            break;
        } // --> missing a default option.
        petArray[petchoice].print();
        // --> Are you really interested only in petArray[0] health at every iteration?
        health_check = petArray[0].check_health();
        // --> QUESTION: I need a random event to happen to a random pet
        // --> You need to choose your pet randomly here.
        // --> Suggestion: you need another function which returns a random number.
        // -->             That function doesn't need to be a member function.
        petArray[0].randomevent();
    } // --> 1) The exit option is stored in "petchoice", not in "choice"
      // --> 2) Use a loop to check all "health"(s) and break if needed
      while (   choice != 0
             && (   !petArray[0].check_health()
                 || !petArray[1].check_health()
                 || !petArray[2].check_health()
                 || !petArray[3].check_health() ) );
    cin.ignore();
    cout << "Press enter to exit." << endl;
    cin.ignore();
    return 0;
}

/* Constructor, creates a new pet with starting values. */
pet::pet() {

    hunger = 50;
    happy = 50;
    sleepy = 50;
}
/* Member function play(), allows playing with a pet. */
void pet::play() {
    int choice = 0;     // --> You are hiding a global variable here.
                        // --> Another good reason to avoid global variables.
    cout << "What should we play?\n";
    cout << "Fetch (1) \nRoll over (2) \n";
    cin >> choice;
    switch (choice) {
    case(1):
        cout << "\nGo get the ball! Fetch!\n";
        happy += 10;
        hunger += 5;
        break;
    case(2):
        cout << "\nCome on, roll over!\n";
        happy += 5;
        hunger += 1;
        break;
    default:
        cout << "Not a valid choice." << endl;
    }
}

/* Member function feed(), allows the user to feed a pet. */
void pet::feed()
{
    int choice = 0;
    cout << "What do you want to feed your pet?\n";
    cout << "1 - Health Food\n2 - Treat\n";
    cin >> choice;
    switch (choice) {
    case(1):    // --> parenthesis aren't a problem, but they are not required.
        cout << "I feel great!\n";
        happy -= 1;
        hunger -= 10;
        break;
    case(2):
        cout << "\nMMM, Yummy!\n";
        happy += 5;
        hunger -= 3;
        break;
    default:
        cout << "Invalid choice." << endl;
    }
}

/* Member function nap(), allows the user to have their pet take a nap. */
void pet::nap()
{
    cout << "Your pet is taking a nap! So relaxing!\n";
    hunger += 10;
    sleepy -= 20;
    happy += 5;
}

/* Member function print(), prints information about a pet. */
void pet::print() {
    cout << "\nYour pet " << name << " is \n";
    cout << "Happy: " << happy << endl;
    cout << "Hungry: " << hunger << endl;
    cout << "Sleepy: " << sleepy << endl;
}

/* Member function check_health(), checks the health of a pet. */
int pet::check_health() {
    if (hunger >= 100) {
        cout << "\nYour pet has starved.\n";
        return 1;
    }
    if (happy <= 0) {
        cout << "\nYour pet has died of a broken heart.\n";
        return 1;
    }
    if (sleepy >= 100) {
        cout << "\nYour pet has perished of exhaustion.\n";
        return 1;
    }
    return 0;
}

// --> This is a member function: it should not decide which class instance modify!!
void pet::randomevent()
{
    int petRandom = rand() % 4;
    int choice = rand() % 4;
    switch (choice) {
    case 0:
        cout << "\n" << petArray[petRandom].name << " was sprayed by a skunk!\n";
        happy -= 20;
        break;
    case 1:
        cout << "\n" << petArray[petRandom].name << " is happy to be with you!\n";
        happy += 10;
        break;
    case 2:
        cout << "\n" << petArray[petRandom].name << " ate some human food and threw up!\n";
        happy += 5;
        hunger += 30;
        break;
    case 3:
        cout << "\n" << petArray[petRandom].name << " was barking all night!\n";
        sleepy += 20;
        break;
    }
}

Topic archived. No new replies allowed.