Need help with adding a 'save to a file function' with pet program.

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?

Here is my code so far:

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
#include"iostream"
#include"fstream"
#include"string"
using namespace 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <fstream>
#include <string>
using namespace std;
bool Save(string FileName)
{
    ofstream File(FileName);
    
    if(File.good())
     {
        File<<Hunger<<endl;
        File<<Happy<<endl;
        File<<name.c_str()<<endl;
        return true;
     }
     else
     {
         cout<<"Failed to open specified file."<<endl;
         return false;
      }
};


If you want to implicitly close the file, you can do so, but the ofstream class destructor closes the file by default. otherwise you would add:

File.close();

to the end before returning true.


Last edited on
Topic archived. No new replies allowed.