Like the title says, I have been working on some code for an assignment lately, and I have hit a bit of a snag. Here is what the assignment calls for:
Write a new save function that's a member of the pet class to do the following:
When the user exit's it should open a file with the same name as the name of the pet.
It should save the pet's data.
Don't forget to close the file after the data is saved
This is confusing me quite a bit. Could anyone point me in the general direction?
#include"iostream"
#include"fstream"
#include"string"
usingnamespace std;
class pet{
private:
int hunger; // private data member
int happy; // private data member
string name; // private data member
public:
pet(); // constructor
void play(); // public member function
void feed(); // public member function
void print(); // public member function
int check_health(); // public member function
};
int main()
{
pet pet1;
int choice = 3;
int health_check = 0;
do{
pet1.print();
cout << "What would you like to do with your pet?\n";
cout << " Play (1) \n Feed (2) \n Exit (0) \n";
cin >> choice;
switch(choice){
case 1:
pet1.play();
break;
case 2:
pet1.feed();
break;
case 0:
cout << "Let's play more later!" << endl;
break;
default:
cout << "Not a valid choice" << endl;
}
health_check = pet1.check_health();
}while(choice != 0 && health_check != 1);
return 0;
}
/* Constructor, creates a new pet with starting values. */
pet::pet(){
int choice;
cout << "Is this and old or new pet? (1) old, (2) new: " << endl;
cin >> choice;
if(choice==1){
ifstream petFile("oldDog.txt");
cout << "Pets we have on file: Roland or roland" << endl;
cout << "Enter the dogs name: " << endl;
cin >> name;
while(name != "Roland" && name != "roland"){
cout << "There is no pet by that name. Please try again." << endl;
cin >> name;
}
while(petFile >> hunger >> happy){};
}else{
hunger = 50;
happy = 50;
cout << "Pet's name? (One word) ";
cin >> name;}
}
/* Member function play(), allows playing with a pet. */
void pet::play(){
int choice = 0;
cout << "What should we play?\n";
cout << " Fetch (1) \n Sit (2) \n Backflip(3) \n";
cin >> choice;
switch(choice){
case(1):
happy += 10;
hunger += 5;
cout << "Here, you dropped this...." << endl;
break;
case(2):
happy += 5;
hunger += 1;
cout << "Yeah I can sit. So what? You wanna fight about it?" << endl;
break;
case(3):
happy += 10;
hunger += 15;
cout << "I gots backflips for days, yo!" << endl;
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 kind of food would you like to feed him?\nGeneric food(1)\nExpensive food(2)" << endl;
cin >> choice;
switch(choice){
case(1):
happy += 5;
hunger -= 5;
cout << "\nMMM, Yummy!\n";
break;
case(2):
happy += 10;
hunger -= 20;
cout << "It's BACON!!!!" << endl;
}}
/* Member function print(), prints information about a pet. */
void pet::print(){
cout << "\nYour pet " << name << " is " << endl;
cout << "Happy: " << happy << endl;
cout << "Hungry: " << hunger << 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 (hunger <= 0){
cout << "\nYour pet has been overfed and no longer wants to play. \n";
return 1;
}
if(happy <= 0){
cout << "\nYour pet has died of a broken heart.\n";
return 1;
}
return 0;
}
Well, in your pet class you'll need to write a function that saves the member data.
void Save();
since the instructions say to save each file as an individual pet, you don't need to pass an external ofstream file pointer to the class: just have the class open its own file, save the data, then exit.