Save not working

Ok im not sure if im doing the save right, it isnt working, it should save the progree but it isnt.

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
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        void Save();
        void Load();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 0;
            NumDef = 0;
            PKill = 0;
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
        ofstream file;

};


int main()
{
    CLASS CO;

    string choice;

    cout << "\n";
    cout << "DINOSAUR ARENA" << endl;
    cout << "\n";
    cout << "Type in the number next to the choice" << endl;
    cout << "\n";
    cout << "1) Enter Game" << endl;
    cout << "2) Help" << endl;
    cout << "3) View Stats" << endl;
    cout << "4) Save game progress" << endl;
    cin >> choice;

    if(choice == "1")
    {
        CO.Player();
    }
    else if(choice == "2")
    {
        CO.Help();
    }
    else if(choice == "3")
    {
        CO.Stats();
    }
    else if(choice == "4")
    {
        CO.Save();
    }
    else
    {
        main();
    }
}


void CLASS::Help()
{
    cout << "HELP SECTION" << endl;
    cout << "\n";
    cout << "1) " << endl;
}


void CLASS::Stats()
{
    string choice;

    Load();

    cout << "\n";
    cout << "STATS" << endl;
    cout << "\n";
    cout << "Type in: back to go back to main menu." << endl;
    cout << "\n";
    cout << "Your total XP: " << XP << endl;
    cout << "Times you were killed: " << PKill << endl;
    cout << "Number of dinosaurs defeated: " << NumDef << endl;
    cin >> choice;

    if(choice == "Back" || choice == "back")
    {
        main();
    }
}


void CLASS::Save()
{
    ofstream file("stats.txt");

    file << XP << endl;
    file << PKill << endl;
    file << NumDef << endl;
}


void CLASS::Load()
{
    ifstream file("stats.txt");

    file >> XP;
    file >> PKill;
    file >> NumDef;
}


void CLASS::Player()
{
    string choice;

    cout << "\n";
    cout << "\n";
    cout << "What weapon will you use?" << endl;
    cout << "\n";
    cout << "1) Shotgun = 4 Damage" << endl;
    cout << "2) Pistol = 6 Damage" << endl;
    cout << "3) Knife = 5 Damage" << endl;
    cout << "\n";

    while(true)
    {
        cin >> choice;

        if(TRHealth <= 0)
        {
            cout << "You defeated the T-Rex!" << endl;
            XP += 30;
            NumDef += 1;
            file << XP << endl;
            file << NumDef << endl;
            cout << "XP gained: " << XP << endl;
            cout << "\n";
            cout << "\n";
            main();
            break;
        }

        if(PHealth <= 0)
        {
            cout << "You were killed!" << endl;
            cout << "XP gained: 0" << endl;
            cout << "\n";
            cout << "\n";
            PKill += 1;
            file << PKill << endl;
            main();
            break;
        }

        if(choice == "1")
        {
            cout << "\n";
            cout << "You used the shotgun!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 4;
            TRex();
        }
        else if(choice == "2")
        {
            cout << "\n";
            cout << "You used the pistol!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 6;
            TRex();
        }
        else if(choice == "3")
        {
            cout << "\n";
            cout << "You used the knife!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 5;
            TRex();
        }
    }
}


void CLASS::TRex()
{
    time_t T;
    time(&T);
    srand(T);

    int Time;

    Time = rand() % 4;

        switch(Time)
        {
            case 0:
                cout << "\n";
                cout << "T-Rex used Bite! = -6 Health" << endl;
                PHealth -= 6;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 1:
                cout << "\n";
                cout << "T-Rex used Chomp! = -5 Health" << endl;
                PHealth -= 5;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 2:
                cout << "\n";
                cout << "T-Rex used Stomp! = -4 Health" << endl;
                PHealth -= 4;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
            default:
                cout << "\n";
                cout << "Trex's attack missed!" << endl;
                PHealth -= 0;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
        }
}
bump
The file saving is fine, I suspect the problem is you call main in multiple locations.
Line 36 declares ofstream file; u don't u rly shouldn't keep declaring it in each member function...
On line 144 u want to do something like this:
file.open("stats.txt");
Then line 119 u have to close that file. This is true for each of those member functions or else u'll get some weird collisions with the stream still being open.

There's also file.open("stats.txt", ios::app); that u may need as it will allow u to open a file with existing text so that u can add more text to it without erasing everything by default.

Lines 26-28 don't rly do anything because 'file' is not open for writing yet.
Last edited on
Hmm, its still not adding my variables to the text file.
Every time you call main you recreate the CLASS object resetting all the values back to default.
Ah, i see, so how do i got back to main if i cant call it?
A loop in main around the if/else block should be all you need.
Topic archived. No new replies allowed.