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.
/*
03/20/2018
Section 02
Assignment 07
*/
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
usingnamespace 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;
}
}
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. constexprint PETS_NUMBER = 4;
/*
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
usingnamespace 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;
}
}