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
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?
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.
#include<iostream>
#include<fstream>
#include<string>
usingnamespace 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
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).
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.