Saving Strings

Ok, so I've got a new issue with my program. Can anyone point me in the right direction on this one? Tell me where I went wrong?

So the program is an electronic pet, and it saves your pet when your done playing, or you can start with a new pet when you play again.

The problem I'm having is that when I reopen a pet, the numbers are way off... Here is an example of what I get when I reopen a pet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Is this a new pet? Yes (1) , No (2)
2
What is your pets name?  Dog

Your pet Dog is
Happy: 63
Hungry: -1268712568
Energy: 0
Health: 4201184
What would you like to do with your pet?
 Play (1)
 Feed (2)
 Rest (3)
 Exercise (4)
 Vet Visit (5)
 Exit (0)


What its suppose to look like.

1
2
3
4
5
6
7
8
9
10
11
12
Your pet Dog is
Happy: 64
Hungry: 75
Energy: 35
Health: 95
What would you like to do with your pet?
 Play (1)
 Feed (2)
 Rest (3)
 Exercise (4)
 Vet Visit (5)
 Exit (0)


Here is my code:

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class pet{
private:
        int hunger;  // private data member
        int happy;   // private data member
        int health;
        int energy;
        string name; // private data member
public:
        pet(); // constructor
        void play();  // public member function
        void feed();  // public member function
        void vetvisit();
        void rest();
        void exercise();
        void print(); // public member function
        int check_health(); // public member function;

};


int main()
{
        pet pet1;
        int choice = 3;
        int health_check = 0;
        ofstream myfile;
        do{
                pet1.print();
                cout << "What would you like to do with your pet?\n";
                cout << " Play (1) \n Feed (2) \n Rest (3) \n Exercise (4) \n Vet Visit (5) \n Exit (0) \n";
                cin >> choice;
                switch(choice){
                case (1):
                        pet1.play();
                        break;
                case (2):
                        pet1.feed();
                        break;
                case (3):
                        pet1.rest();
                        break;
                case (4):
                        pet1.exercise();
                        break;
                case (5):
                        pet1.vetvisit();
                        break;
                }
                health_check = pet1.check_health();
        }while(choice != 0 && health_check != 1);

        myfile.close();
        return 0;
}

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


        int choice1 = 0;
        cout << "Is this a new pet? Yes (1) , No (2) " << endl;
        cin >> choice1;
        switch(choice1){

  case (1): {
        ofstream myfile;
        hunger = 50;
        happy = 50;
        energy = 50;
        health = 50;
        cout << "Pet's name? (One word) ";
        cin >> name;
        myfile.open (name.c_str());
        cout << "Saving pet to a file."<< endl;
        break;
}

  case (2):
        ofstream myfile;
        cout << "What is your pets name?  ";
        cin >> name;
        myfile.open (name.c_str());
        break;
        }



}

/* 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 Rollover (3) \n Shake (4) \n";
        cin >> choice;
        switch(choice){
        case(1):
                cout << " :-) \n";
                happy += 15;
                hunger -= 15;
                health += 10;
                energy -= 15;
                break;
        case(2):
                cout << " :-) \n";
                happy += 5;
                hunger -= 5;
                health += 2;
                energy -= 2;
                break;
        case(3):
                cout << " :-) \n";
                happy += 10;
                hunger -= 10;
                health += 5;
                energy -= 5;
                break;
        case(4):
                cout << " :-) \n";
                happy += 7;
                hunger -= 7;
                health += 3;
                energy -= 3;
                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 would you like to feed your pet?\n";
        cout << " Dry Food (1) \n Wet Food (2) \n Organic Food (3) \n";
        cin >> choice;
        switch(choice){

        case(1):
                cout << " :-) \n";
                happy += 15;
                hunger += 30;
                health += 10;

                energy -= 20;
                break;
        case(2):
                cout << " :-) \n";
                happy += 30;
                hunger += 30;
                health += 10;
                energy -= 20;
                break;
        case(3):
                cout << " :-) \n";
                happy -= 1;
                hunger += 30;
                health += 20;
                energy -= 20;
                break;
        }
}
void pet::rest(){
        int choice = 0;
        cout << "How long would you like to allow your pet to rest? \n";
        cout << " 1 hour (1) \n Half day (2) \n Overnight (3) \n";
        cin >> choice;

        switch(choice){
        case(1):
                cout << " :-) \n";
                happy += 10;
                hunger -= 10;
                health += 5;

                energy += 10;
                break;
        case(2):
                cout << " :-( \n";
                happy -= 15;
                hunger -= 15;
                health += 10;
                energy += 20;
                break;
        case(3):
                cout << " :-( \n";
                happy -= 30;
                hunger -=20;
                health += 5;
                energy += 40;
                break;
        }
}
void pet::exercise(){
        int choice = 0;
        cout << "What type of exercise? \n";
        cout << " Walk (1) \n Jog (2) \n Run (3) \n";
        cin >> choice;

        switch(choice){
        case(1):
                cout << " :-) \n";
                happy += 3;
                hunger -= 15;
                health += 5;
                energy -= 15;
                break;
        case(2):
                cout << " :-| \n";
                happy -= 5;
                hunger -= 25;
                health += 15;
                energy -= 20;
                break;
        case(3):
                cout << " :-( \n";
                happy -= 10;
                hunger -= 30;
                health += 35;
                energy -= 35;
                break;
        }
}

void pet::vetvisit(){
        cout << "Make sure you are watching your pet closely and doing whats best for your pet!\n";
        cout << " :,-( \n";
                happy -= 40;
                hunger -= 25;
                health += 50;
                energy -= 30;
}

/* 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;
        cout << "Energy: " << energy << endl;
        cout << "Health: " << health << endl;
}

/* Member function check_health(), checks the health of a pet. */
int pet::check_health(){
        if(hunger <= 0){
                cout << "\nYour pet has starved.\n";
                return 1;
        }
        if(happy <= 0){
                cout << "\nYour pet has died of a broken heart.\n";
                return 1;
        }
        if(health <= 0){
                cout << "\nYour pet has become ill and died.\n";
                return 1;
        }
        if(energy <= 0){
                cout << "\nYour pet is too fatigued and died.\n";
                return 1;
        }
        if(hunger <= 20){
                health -= 15;
                happy -= 2;
                energy -= 2;
        }
        if(happy <= 20){
                health -= 15;
                energy -= 2;
                hunger -= 1;
        }
        if(energy <= 20){
                health -= 15;
                happy -= 2;
                hunger -= 1;
        }
        if(health <= 20){
                energy -= 2;
                happy -= 2;
                hunger -= 1;
        }
        if(health > 100){
                health -= 10;
        }
        if(happy > 100){
                happy -= 10;
        }
        return 0;
}
its because your not outputting anything to the text file, to output you need to do:

1
2
3
4
myfile << hunger << endl;
myfile << happy << endl;
myfile << energy << endl;
myfile << health << endl;


your also not getting the values. your trying to output the value again when you load it, you need to use ifstream myfile, not ostream

myfile >> hunger;

etc.
Ok, so myfile << hunger << endl; goes just before myfile.close() right?
yes sandwiched between myfile.open and myfile.close.
Last edited on
This is the error I'm getting after I make those changes.

1
2
3
4
5
6
7
8
9
10
11
-bash-4.1$ g++ pet.cpp
pet.cpp: In function 'int main()':
pet.cpp:63: error: 'hunger' was not declared in this scope
pet.cpp:64: error: 'happy' was not declared in this scope
pet.cpp:65: error: 'energy' was not declared in this scope
pet.cpp:66: error: 'health' was not declared in this scope
pet.cpp: In constructor 'pet::pet()':
pet.cpp:98: error: no match for 'operator<<' in 'myfile.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(&((pet*)this)->pet::hunger))) << std::endl'
pet.cpp:99: error: no match for 'operator<<' in 'myfile.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(&((pet*)this)->pet::happy))) << std::endl'
pet.cpp:100: error: no match for 'operator<<' in 'myfile.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(&((pet*)this)->pet::energy))) << std::endl'
pet.cpp:101: error: no match for 'operator<<' in 'myfile.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(&((pet*)this)->pet::health))) << std::endl'
I changed it all to fstream to make it read and write, still the same problem.

Anyone else have an idea?
I did and it worked for me, the problem is, is that you arent setting the values of health, hunger etc, so the code has no idea what to output, also if your just using classes for variables, then use a struct.
I'm not sure how I'm not setting the values? They start out at a certain number every time, and then when you exit the program it saves the values.
I edited this and it outputs the correct values instead of a bunch of numbers

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
pet::pet(){

        hunger = 20;
        happy = 20;
        health = 50;
        energy = 50;

        int choice1;
        cout << "Is this a new pet? Yes (1) , No (2) " << endl;
        cin >> choice1;

        switch(choice1){

  case 1: {
        ofstream myfile;
        cout << "Pet's name? (One word) ";
        cin >> name;
        myfile.open (name.c_str());
        cout << "Saving pet to a file."<< endl;
        myfile << hunger << endl;
        myfile << happy << endl;
        myfile << energy << endl;
        myfile << health << endl;
        break;
        }

  case 2:
        ifstream myfile;
        cout << "What is your pets name?  ";
        cin >> name;
        myfile.open (name.c_str());

        myfile >> hunger;
        myfile >> happy;
        myfile >> energy;
        myfile >> health;
        break;
        }
}
Ok, I tried that and it gives me the starting values when you start the game, not the values when you last played.
Think it might be something wrong with this area?

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
int main()
{
        pet pet1;
        int hunger;
        int happy;
        int health;
        int energy;

        int choice = 3;
        int health_check = 0;
        ifstream myfile;
        do{
                pet1.print();
                cout << "What would you like to do with your pet?\n";
                cout << " Play (1) \n Feed (2) \n Rest (3) \n Exercise (4) \n Vet Visit (5) \n Exit (0) \n";
                cin >> choice;
                switch(choice){
                case (1):
                        pet1.play();
                        break;
                case (2):
                        pet1.feed();
                        break;
                case (3):
                        pet1.rest();
                        break;
                case (4):
                        pet1.exercise();
                        break;
                case (5):
                        pet1.vetvisit();
                        break;
                }
                health_check = pet1.check_health();
        }while(choice != 0 && health_check != 1);
        myfile >> hunger;
        myfile >> happy;
        myfile >> energy;
        myfile >> health;
        myfile.close();
        return 0;
}
Topic archived. No new replies allowed.