Problem streaming input through two contructors (two classes - using ADTs)

I have a main .cpp file which contains int main(int argc, char** argv) and 2 included personal headers which correspond to their linked implementation files.

What I am trying to do is use ifstream to pass integer values from a text file into my program, have the program execute, and then output those modified integers back into the same text file. I have tested the "program" portion of my project using declared values. I know that my program does what I have intended for it to do, but I can't seem to properly inject any outside data using files.

Here's what I have (not including all of the unrelated objective code):

Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char** argv)
{
        file File;
        std::string gameFile = File.findFile(); // prompts user for existing file
	if(gameFile.size() > 0) { // load saved game
		std::ifstream in;
		in.open(gameFile.c_str());
		game Game(gameFile, in);
		in.close();
	}
	else  // make new game
		game Game;
}


Game.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include "game.h"
#include "player.h"

using namespace std;

game::game() // for new game
{
	loadedGame = "";
	for(int i = 0; i < maxZone; i++)
		zone[i] = i+1;
	Player = new player;
}
game::game(std::string fileName, std::ifstream& data) // for load
{
	loadedGame = fileName;
	for(int i = 0; i < maxZone; i++)
		zone[i] = i+1;
	Player = new player(data);
}
int game::expGained(int eLevel) // Example function used in project
{
        int iLevel = Player.getLevel();
	int xp = 0;
	if(iLevel <= 5)
		xp = (eLevel*3) + (eLevel/iLevel)*4 + 1;
	else
		xp = (eLevel*4) + (eLevel-iLevel)*(2*eLevel);
	if(xp <= 0)
		xp = (eLevel+1)/2;

	return xp;
}


In game.cpp, I am unsure of Player = new player; and Player = new player(data);. I have never tried doing this. This is simply my best guess of how to make this mess work properly. Hopefully I am close?

Game.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef _GAME_H_
#define _GAME_H_

#include "player.h"

class game {
        std::string loadedGame;
	player* Player;
public:
	game(); // default - new game
	game(std::string fileName, std::ifstream& data); // loaded game
};

#endif 


In game.h, I haven't yet tried anything like player* Player;. As stated in my comments above for game.cpp, I have no clue how to go about this.

player.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "player.h"

player::player()
{
	srand(time(0));

	playerLevel = 1;
        playerExp = 0;
	this->stats.attack = rand()% 2 + 2;
	this->stats.defense = rand()% 4 + 1;
	this->stats.health = 15;
	this->stats.speed = rand()% 4 + 1;
}
player::player(std::ifstream& input)
{
	srand(time(0));

	input >> playerLevel;					
	input >> playerExp;					
	input >> this->stats.attack;			
	input >> this->stats.defense;			
	input >> this->stats.health;			
	input >> this->stats.speed;			
}
void player::saveGame(std::string& file)
{
	ofstream out;
	if(file.size() < 1) {
		cout << "\nThis game has not previously been saved. Save file as: ";
		cin >> file;
		cout << "\nYour game, " << file << ", has been saved!\n"; 
		file += ".txt"; }
	else
		cout << "\nYour game has been saved!\n"; // string file includes + ".txt"

	out.open(file.c_str());
	out << playerLevel << endl;				
	out << playerExp << endl;							
	out << this->stats.attack << endl;			
	out << this->stats.defense << endl;			
	out << this->stats.health << endl;			
	out << this->stats.speed << endl;				
	out.close();
}


In player.cpp, I really have no clue where to start with the issue. I'm not even exactly sure if any values are being passed to these variables. Though, I honestly haven't taken much time to problem solve. I don't want to waste a lot of time just to find out that my attempt is incorrect and/or unconventional.

player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef _PLAYER_H_
#define _PLAYER_H_

class player {
public:
	player(); // default
	player(std::ifstream& input); // loaded game gata
        void saveGame(std::string& file);
        int getLevel() const {return playerLevel;}

private:
	int playerLevel;
	int playerExp;
	struct statistics {
		int health,
		    speed,
		    attack,
		    defense,
		    damage;
        } stats;	
};

#endif 



So really, my concerns are:

Is my attempt mostly correct? If not, why?
Are there any alternatives that may be easier or more understandable?
What is something I can do that will help me in the future with similar issues?

I greatly appreciate all advice and help.
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>

struct player
{
    explicit player( const std::string& file_mame = "" )
    {
        if( file_mame.empty() ) { /* default initialize */ }
        else
        {
            std::ifstream file(file_mame) ;
            // load data from file
        }
    }
    void save( const std::string& file_mame )
    {
        if( !file_mame.empty() )
        {
            std::ofstream file(file_mame) ;
            // save data to file
        }
    }

    // ...
};

struct game
{
    explicit game( const std::string& file_name = "" )
          : file_name(file_name), the_player(file_name) {}

    void save() { the_player.save(file_name) ; }

    void save( const std::string& file_name )
    { the_player.save( game::file_name = file_name ) ; }

    // ...

    private:
       std::string file_name ;
       player the_player ; // *** by value
};

int main()
{
    // ...
    std::string game_file ;
    // ...
    bool game_file_exists /* = check if file exists ... */ ;
    game the_game( game_file_exists ? game_file : "" ) ;

    // play game
}

Topic archived. No new replies allowed.