Ask the user if this is a new or old (i.e. saved) pet.
If it is a new pet the constructor should work as before.
If it is an old pet the constructor should ask for the old pet's name. Then the constructor should open the file with that name and read in the pet's data.
I've been trying different things to get this to work, but I'm not having any such luck. Can anyone help me out with this? What am I doing wrong? What do you suggest? Any help would be greatly appreciated.
I'm a hands on learner, and learn by example, so I'm struggling with this class as my instructor does not do that very well.
I'm having problems with writing a new file when a new pet is made, and recalling that file later if the user comes back later to play and wants to recall their pet.
This is not working, I know its wrong, I get errors telling me so... just not sure what to do anymore.
I put in the breaks and replaced myfile.open(name) with myfile.open (name.c_str())
I'm still getting errors though.
1 2 3 4 5
pet.cpp: In constructor 'pet::pet()':
pet.cpp:89: error: jump to case label
pet.cpp:84: error: crosses initialization of 'std::ofstream myfile'
pet.cpp:92: error: redeclaration of 'std::ofstream myfile'
pet.cpp:84: error: 'std::ofstream myfile' previously declared here
ok, nevermind, I fixed that part. I moved ofstream myfile; to before the do statement, that worked. Problem I have now is once you either put in the pets name, or try to recall the old pet, it just asks you again if this is a new pet or an old pet, and it just does it over and over again instead of running the program...
Should I not use a switch to determine if it is a new pet or an old pet?
.. or you can create a new scope by putting the case code inside { }.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
switch(choice1) {
case 1: {
hunger = 50;
happy = 50;
energy = 50;
health = 50;
cout << "Pet's name? (One word) ";
cin >> name;
ofstream myfile;
myfile.open(name.c_str());
cout << "Saving pet to a file."<< endl;
}
case 2: {
cout << "What is your pets name? ";
cin >> name;
ofstream myfile;
myfile.open(name.c_str());
}
}
Note that a version of open and constructor taking std::string as argument was added for ifstream/ofstream in C++11. You compiler is too old to support this.
The condition for the loop is choice == 1 || 2
Given that if you enter either of them the condition is true and the loop is executed again, the correct syntax is
choice == 1 || choice == 2
The way you wrote it is interpreted as while(choice == 1 or true), because every value except 0 evaluates to true
I fixed the choice, but it still just loops this section and won't move on to the rest of the program...
This is what I get after that:
1 2 3 4 5 6 7 8 9 10
-bash-4.1$ ./a.out
Is this a new pet? Yes (1) , No (2)
1
Pet's name? (One word) Dog
Saving pet to a file.
Is this a new pet? Yes (1) , No (2)
2
What is your pets name? Dog
Is this a new pet? Yes (1) , No (2)
^C
Ok, the syntax is correct, but like I said, if you enter 1 in choice the loop condition will be true and the loop will continue. You want the loop to continue only if the user enters an invalid choice, so it should be while (choice1 != 1 || choice1 != 2)
Ok, that didn't work either, I just removed the whole do process and it works.
My problem now is it is saving the numbers of each category all wrong...
-bash-4.1$ ./a.out
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)
2
What would you like to feed your pet?
Dry Food (1)
Wet Food (2)
Organic Food (3)
2
:-)