ifstream/ofstream to save variables

This is a discussion on ifstream/ofstream to save variables within the C++ Programming forums, part of the General Programming Boards category; Hi I am haphazardly learning C++ on my own/through tutorials I am trying to write a program that has you ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    ifstream/ofstream to save variables

    Hi
    I am haphazardly learning C++ on my own/through tutorials
    I am trying to write a program that has you input values, writes those values to a .txt file, and then can read them/assign them back to the variables if you execute it again (i.e. saving/loading)

    The relevant pieces of code are

    Code:
    #include<iostream>
    #include<fstream>
    
    #define screensize 25
    
    using namespace std;
    
    void clearScreen()
    {
    	for (int x = 0; x < screensize; x++)
    		cout << "\n";
    	return;
    } 
    
    class Hero
    {
    	public:
    	string name;
    	int hp;
    	int mp;
    	int gold;
    	int exp;
    	int level;
    
    	void setName (string x)
    	{
    		name = x;
    		return;
    	}
    
    	void setValues (int a, int b, int c, int d, int e)
    	{
       		hp = a;
       		mp = b;
       		gold = c;
       		exp = d;
       		level = e;
    		return;
    	}   	
    
     	void showStats()
    	{
       		 clearScreen();
       		 cout << "\t\t\tSTATS" << endl;
    		 cout << "Name : " << name << endl;
      		 cout << "Level: " << level << endl;
       		 cout << "HP: " << hp << endl;
       		 cout << "MP: " << mp << endl;
       		 cout << "EXP: " << exp << endl;
       		 cout << "Gold: " << gold << endl;
       		 cout << "<Press enter to return!>";
       		 cin.get();
       		 return;
    	} 
    };
    
    void saveGame(Hero player)
    {
    	ofstream save ("save.txt", ios::trunc);
    	save << player.name << " ";
    	save << player.hp << " ";
    	save << player.mp << " ";
    	save << player.gold << " ";
    	save << player.exp << " ";
    	save << player.level << " ";
    	save.close();
    	return;
    }
    
    void loadGame(Hero player)
    {
    	string x;
    	int a, b, c, d, e;
    	ifstream load ("save.txt");
    	load >> x >> a >> b >> c >> d >> e;
    	player.setName(x);
    	player.setValues(a, b, c, d, e);
    	load.close();
    	player.showStats();
    	return;
    }
    
    int main()
    {
    	clearScreen();
    	Hero player;
    	cout << "1. Start" << endl;
    	cout << "2. Load" << endl;
    	int selection;
    	cin >> selection;
    	
    	switch (selection)
    	{
    		case 1:
    			clearScreen();
    			cout << "Name: ";
    			cin >> player.name;
    			cout << "Level: ";
    			cin >> player.level;
    			cout << "HP: ";
    			cin >> player.hp;
    			cout << "MP: ";
    			cin >> player.mp;
    			cout << "Gold: ";
    			cin >> player.gold;
    			cout << "EXP: ";
    			cin >> player.exp;
    			
    			cout << "<Press enter to check your stats page!>";
    			cin.get();
    			cin.get();
    			player.showStats();
    			clearScreen();
    			cout << "Saving stats...";
    			saveGame(player);
    			break;
    		case 2:
    			loadGame(player);
    			break;
    	}
    }
    Now this works, however
    If I call player.showStats() from main instead of loadGame, like so
    Code:
    case 2:
    			loadGame(player);
                            player.showStats();
    			break;
    It outputs (seemingly) random large values instead of the ones previously set
    What does this mean
    Last edited by Donarstan; 09-30-2008 at 08:08 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Changes made in a function, stay in the function. So the changes to player made in showStats stay there, and aren't "real". If you want changes made to a variable to "stick", you need to pass by reference instead of by copy (that is, by doing void loadGame(Hero &player)).

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Aaaah, thanks a lot
    I had actually considered a pass by reference/by copy issue but had erroneously narrowed the problem to setValues instead of loadGame :s

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So you are trying to serialize and de-serialize objects to/from disk? You can overload the insertion and extraction operators relative to a class which makes serialization a snap.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Later have a look at boost::serialize. It makes serialization very productive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 07:01 PM
  2. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 02:42 PM
  3. How does one save variables to a file?
    By madgolfertom in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2004, 04:30 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Save, NOT save as
    By Lurker in forum Windows Programming
    Replies: 5
    Last Post: 05-30-2003, 01:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21