   Test Your Debugging Skills with my Source Code Problem
  This small roleplay project basically loads the player's age and saves it. Even though it works, you'll see below that before load/save is called, it sets his age to the default 22. That's fine, but if the player saves again and he ages, that default 22 will replace 23 which is very bad. I tried commenting it out, but it said..C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(36) : warning C4700: local variable 'Player' used without having been initializedLinking...Pract1.exe - 0 error(s), 1 warning(s)Feel free to share your ideas. Yes, I'm using fstream.h (so evil) but I downloaded the upgrade processor package but it didn't work after two loading twice. If/Else below can be a switch, so I know that already. But the main issue is the Player.setAge(22) part. Should it stay in there? I'm not sure. That's where you come in. ps. I'm thinking Player.setAge(22) would be defaulted for a new game. When loading, it'll set a new age. When saving, it'll save the current age. I guess I need confirmation.So any help is appreciated. Thanks // Include    ---------------------------------#include <iostream>#include <fstream.h>// Classes    ---------------------------------class commoner{public:unsigned short getAge() const { return itsAge; }void setAge(unsigned short a_age) { itsAge = a_age; }private:unsigned short itsAge;};// Prototypes ----------------------------------void save(commoner Player);void load(commoner Player);// Main  ---------------------------------unsigned short main(void){using std::cout;using std::cin;using std::endl;commoner Player;Player.setAge(22);// Load or Save?cout << "Load or Save?" << endl;cout << "1) Load" << endl;cout << "2) Save" << endl;cout << "3) Exit" << endl;cout << "Choice: ";int Choice;cin >> Choice;if (Choice == 1) {  load(Player);  cout << Player.getAge() << endl; }if (Choice == 2) {save(Player);}if (Choice == 3) {exit(1);}else{system("pause");   system("cls");main(); }return 0;}// Function: Save  -----------------------------void save(commoner Player){using std::cout;using std::cin;using std::endl;ofstream fout("a:test.dat", ios::binary);if (fout.is_open()){fout.write((char *) (&Player), sizeof(Player));fout.close();cout << "Your Game is Saved!" << endl;}else{cout << "An Error occured while saving." << endl;}}// Function: Load  ---------------------------------void load(commoner Player){using std::cout;using std::cin;using std::endl;ifstream fin("a:test.dat", ios::binary);if (fin.is_open()){fin.read((char *) (&Player), sizeof(Player));fin.close();cout << "Saved Game is Open!" << endl;}else{cout << "An Error occured while loading." << endl;}}    http://phil.webula.netAIM: wuZZup189[edited by - philvaira on February 15, 2004 11:30:40 PM][edited by - philvaira on February 15, 2004 11:33:57 PM]
  You''re passing the object to your load() and save() functions by value, which will create a copy of the object to load and save. Try changing the declaration to:void load(commoner & Player);This will pass the objects by reference, and so will properly modify the object being passed. ;  Should I do this too?void save(commoner & Player);And is Player.setAge(22); fine in the beginning? I'm figuring it's just making defaults if starting a new game.. ?   UPDATE: I made it so it shows the current age before I save. When it goes back to main, it loads the default age because its seeing Player.setAge(22);*crys*http://phil.webula.netAIM: wuZZup189   [edited by - philvaira on February 15, 2004 12:06:39 AM];  Yes. . . actually save should have a const in there too, because the act of saving it shouldn''t change it. ;  THis is odd...Age: 22I enter new age: 23Save.Goes back to main().Current Age: 22Any idea?   http://phil.webula.netAIM: wuZZup189 ;  To allow you to better understand the problem I posted above this one, I made an easier script to look at. I need a default for the beginning of the program, but when I go back, it sees int Age = 22, so it puts it back. #include <iostream>#include <fstream>void Save(int Age);void Load(int Age);int main(){using std::cout;using std::cin;using std::endl;int Age = 22;cout << "Current Age: " << Age << endl;cout << "Enter New Age: ";cin >> Age;cout << "\n1) Save" << endl;cout << "2) Load" << endl;cout << "3) Exit" << endl;cout << "Choice: ";int Choice;cin >> Choice;switch(Choice){case 1: Save(Age);main();case 2: Load(Age);main();case 3: exit(1);break;default: cout << "Exiting..." << endl;break;}return 0;}void Save(int Age){using std::cout;using std::endl;using std::cin;std::ofstream fout("a:test.dat");if (fout.is_open()){fout << Age << "\n";fout.close();cout << "Saving Complete!" << endl;}else{cout << "Error." << endl;}}void Load(int Age){using std::cout;using std::endl;using std::cin;std::ifstream fin("a:test.dat");if (fin.is_open()){fin >> Age;fin.close();cout << "Finished Loading." << endl;}else{cout << "Error." << endl;}} http://phil.webula.netAIM: wuZZup189 ;  I made some changes. Seems to work. By the way, recursion isn''t a very good idea, there. Play enough and you''ll get a stack overflow. // Include    ---------------------------------#include <iostream>#include <fstream>// Classes    ---------------------------------class commoner {  public:   unsigned short getAge() const { return itsAge; }   void setAge(unsigned short a_age) { itsAge = a_age; }  private:   unsigned short itsAge; };// Prototypes ----------------------------------int save(const commoner &Player);int load(commoner &Player);// Main  ---------------------------------int main(void) {  commoner Player;  Player.setAge(22);    while(1)   {    // Load or Save?    std::cout << "Load or Save?" << std::endl    << "1) Load" << std::endl    << "2) Save" << std::endl    << "3) Exit" << std::endl    << "4) Default" << std::endl    << "5) Set Age" << std::endl    << "6) Show Age" << std::endl    << "Choice: ";   int Choice;    std::cin >> Choice;   switch(Choice){ case 1:  if(!load(Player))   std::cout << "Error loading player." << std::endl; break;  case 2:  if(!save(Player))   std::cout << "Error saving player." << std::endl; break;  case 3:  exit(0); break;  case 4:  Player = commoner(); break;  case 5:  std::cout << "Enter age: ";  std::cin >> Choice;  Player.setAge(Choice); break;  case 6:  std::cout << "Age is " << Player.getAge() << std::endl; break;  default:  std::cout << "Invalid selection." << std::endl;}   } return 0;}// Function: Save  -----------------------------int save(const commoner &Player) {  std::ofstream fout("test.dat", std::ios::binary);    if(fout.is_open())   {    fout.write((char *) (&Player), sizeof(Player));    return 1;   }    return 0; }// Function: Load  ---------------------------------int load(commoner &Player) {  std::ifstream fin("test.dat", std::ios::binary);    if(fin.is_open())   {    fin.read((char *) (&Player), sizeof(Player));    return 1;   }    return 0; };  This may sound stupid but I''m new here. I copy/paste and it shows up as one line in any text program. Is this the only way to copy/paste?   http://phil.webula.netAIM: wuZZup189 ;  Opps, sorry, that was the editor I used. IE doesn''t convert unix style newlines, and NotePad doesn''t display them. Blame Microsoft Here it is again converted into a more Microsoft friendly format: // Include    ---------------------------------#include <iostream>#include <fstream>// Classes    ---------------------------------class commoner {  public:   unsigned short getAge() const { return itsAge; }   void setAge(unsigned short a_age) { itsAge = a_age; }  private:   unsigned short itsAge; };// Prototypes ----------------------------------int save(const commoner &Player);int load(commoner &Player);// Main  ---------------------------------int main(void) {  commoner Player;  Player.setAge(22);    while(1)   {    // Load or Save?    std::cout << "Load or Save?" << std::endl    << "1) Load" << std::endl    << "2) Save" << std::endl    << "3) Exit" << std::endl    << "4) Default" << std::endl    << "5) Set Age" << std::endl    << "6) Show Age" << std::endl    << "Choice: ";   int Choice;    std::cin >> Choice;   switch(Choice){ case 1:  if(!load(Player))   std::cout << "Error loading player." << std::endl; break;  case 2:  if(!save(Player))   std::cout << "Error saving player." << std::endl; break;  case 3:  exit(0); break;  case 4:  Player = commoner(); break;  case 5:  std::cout << "Enter age: ";  std::cin >> Choice;  Player.setAge(Choice); break;  case 6:  std::cout << "Age is " << Player.getAge() << std::endl; break;  default:  std::cout << "Invalid selection." << std::endl;}   } return 0;}// Function: Save  -----------------------------int save(const commoner &Player) {  std::ofstream fout("test.dat", std::ios::binary);    if(fout.is_open())   {    fout.write((char *) (&Player), sizeof(Player));    return 1;   }    return 0; }// Function: Load  ---------------------------------int load(commoner &Player) {  std::ifstream fin("test.dat", std::ios::binary);    if(fin.is_open())   {    fin.read((char *) (&Player), sizeof(Player));    return 1;   }    return 0; };  Hehe I had to convert it, but I got it runing & it works well. Thanks dude.   http://phil.webula.netAIM: wuZZup189 