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)
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 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.
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;
}