fstream issues, please help?

So I'm having some fstream issues, not sure what I am doing wrong. In my program I have it save the values before exiting, but when I try to reopen the file the values are way off. Weird thing is, if I try to make a new file with the same name, it will open the values that were previously saved. Can anyone help me out here?

Here's what I get:

Is this a new pet? Yes (1) , No (2)
1
Pet's name? (One word) joel
Saving pet to a file.

Your pet joel is

Your pet joel is
Happy: 50
Hungry: 50
Energy: 50
Health: 50
Money :$50
What would you like to do with your pet?
Play (1)
Feed (2)
Rest (3)
Exercise (4)
Vet Visit (5)
Work (6)
Exit (0)
0
-bash-4.1$ ./a.out
Is this a new pet? Yes (1) , No (2)
2
What is your pets name? joel

Your pet joel is

Your pet joel is
Happy: 32767
Hungry: 1696601224
Energy: 0
Health: 4200261
Money :$-2.09531e-07
What would you like to do with your pet?
Play (1)
Feed (2)
Rest (3)
Exercise (4)
Vet Visit (5)
Work (6)
Exit (0)
0

Your pet is too fatigued and died.
Last edited on
edit
Last edited on
another edit
Last edited on
No one has an idea?
I've tried everything I can think of... I could really use some help please?
What I would recommend for debugging this is to make a much smaller program that only deals with the writing and reading from files. That will make debugging easier for you and for us.

From what I saw, the values that is displayed seems to be random, which indicates the value is uninitialized. After making your smaller example, I would look for that.

Finally, isn't ifstream and ofstream reversed in this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  case (1): {
        ifstream ifs;
        hunger = 50;
        happy = 50;
        energy = 50;
        health = 50;
        money = 50.00;
        cout << "Pet's name? (One word) ";
        cin >> name;
        ifs.open (name.c_str());
        cout << "Saving pet to a file."<< endl;
        ifs >> hunger >> happy >> energy >> health >> money;
        break;
}

  case (2):
        ofstream ofs;
        cout << "What is your pets name?  ";
        cin >> name;
        ofs.open (name.c_str());
        ofs << hunger << happy << energy << health;
        break;
        }


In case 1, you want to save (write) to a file, so you should used ofstream. In case 2, you want to load (read) from a file, so you should use ifstream. Unless I completely misinterpreted what you're doing there.
Ok, here we go. I changed it and I'm still getting the same results.

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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class pet{
private:
        int hunger;
        int happy;
        int health;
        int energy;
        float money;
        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 work();
        void exit();
        void print(); // public member function
        int check_health();



};

int main()
{
        pet pet1;
        float sum;
        ifstream ifs;


        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 Rest (3) \n Exercise (4) \n Vet Visit (5) \n Work (6) \n Exit (0) \n";
                cin >> choice;
                switch(choice){

                }
                health_check = pet1.check_health();
        }while(choice != 0 && health_check != 1);

        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 ofs;
        hunger = 50;
        happy = 50;
        energy = 50;
        health = 50;
        money = 50.00;
        cout << "Pet's name? (One word) ";
        cin >> name;
        ofs.open (name.c_str());
        cout << "Saving pet to a file."<< endl;
        ofs << hunger << happy << energy << health << money;
        break;
}

  case (2):
        ifstream ifs;
        cout << "What is your pets name?  ";
        cin >> name;
        ifs.open (name.c_str());
        ifs >> hunger >> happy >> energy >> health;
        break;
        }



}

/* 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;
        cout << "Money :$" << money << 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;
               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;
}


Here's the outcome of the program.
Is this a new pet? Yes (1) , No (2)
1
Pet's name? (One word) joel
Saving pet to a file.

Your pet joel is

Your pet joel is
Happy: 50
Hungry: 50
Energy: 50
Health: 50
Money :$50
What would you like to do with your pet?
Play (1)
Feed (2)
Rest (3)
Exercise (4)
Vet Visit (5)
Work (6)
Exit (0)
0
-bash-4.1$ ./a.out
Is this a new pet? Yes (1) , No (2)
2
What is your pets name? joel

Your pet joel is

Your pet joel is
Happy: 32767
Hungry: 1696601224
Energy: 0
Health: 4200261
Money :$-2.09531e-07
What would you like to do with your pet?
Play (1)
Feed (2)
Rest (3)
Exercise (4)
Vet Visit (5)
Work (6)
Exit (0)
0

Your pet is too fatigued and died.
Last edited on
You say you changed it but it doesn't look like you switched the ifstream and ofstream.
Oh, sorry, I changed it in the larger program, not the condensed one, sorry, I will make the changes.
The issue is probably because when you write to the file, there is no whitespace between values. The >> operator reads until a whitespace by default. Btw, I used the tool valgrind to debug the problem, if you want to look into that tool (it's for linux, not sure if there's an equivalent for windows).
Last edited on
yeah I figured that part out... I actually had a dream last night and it came to me.... weird...

Anyway, so I open the file outside of the program and the variables are right, but when I go to open the file in the program they are no where close to being right. So obviously somethings wrong with my file read function... I just don't know what.
Topic archived. No new replies allowed.