Trouble when load file from txt

So yeah, I have asked about this before, this time I manage to create a save file, load the setting of the game but some how I am struggling with loadding the player information.

I think the problem is in how I put the variable player[] in the function but I do not know how to fix it.

I will only post the struct and the function because it's where the problem lies.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  // Setting struct //
struct setting_game
{
	int size, condition, timer, color;
	char mode;
	bool sound;
};
setting_game setting;

struct player_information
{
	bool AI;
	char sign;
	int difficulty, victorytime;
	int check[5000];
};	
player_information player[3];
// savegame //
void savegame(int playerturn, int turn)
{
	ofstream file;
    file.open("loaddata.txt");
    
 	file << 1 << endl;
    file << setting.size << endl; 
    file << setting.mode << endl; 
    
    file << setting.condition << endl; 
    file << setting.sound << endl; 
    file << setting.timer << endl; 
    file << setting.color << endl; 
    file << turn << endl; 
    
    for (int j = 1; j <= 2; j++)
    {
	    file << player[j].AI << endl; 
	    file << player[j].difficulty << endl; 
	    file << player[j].sign << endl; 
	    file << player[j].victorytime << endl; 
	    
	    for (int i = 1; i <= playerturn; i++)
	    	file << player[j].check[i] << endl;
    }
							 
    file.close();  
    exit();
}	  

// Loadgame //
int loadgame(int &playerturn, int &turn, player_information player[3])
{
	ifstream file("loaddata.txt");
	if (file.is_open())
	{
		file >> line;
		if (line) 
	    {   
			cout << "You have a save file, do you want to load it?\n";
	        cout << "1. Yes \n2. No\n";
	        cin >> choice;
	    }  
	    else return 0;
	    if (choice == 1)
        {
        	file >> setting.size; 
		    file >> setting.mode; 
		    
		    file >> setting.condition; 
		    file >> setting.sound; 
		    file >> setting.timer; 
		    file >> setting.color; 
		    file >> turn; 
		    
		    for (int j = 1; j <= 2; j++)
		    {
			    file >> player[j].AI; 
			    file >> player[j].difficulty; 
			    file >> player[j].sign; 
			    file >> player[j].victorytime; 
			    
			    for (int i = 1; i <= playerturn; i++)
			    	file >> player[j].check[i];
		    }
		}
		else return 0;
		file.close();  
    	return 1;		        	
		        	
	}
	file.close();  
    return 0;	
}
right away I spot that you loop over playerturn to write into the file, but you did NOT WRITE playerturn's data into the file, so the reader has NO CLUE how many loops were written. You need to write it into the file before the loop, then you can read it and refer to its value to loop and read the array data.
-> it is OK for the other loop which is always 2 iterations.
Last edited on
I'm not quite understand what are you saying, can you specify the line and tell me how to fix?
file << playerturn; //add this @ 33
file >> playerturn; //add this @ 73

those 2 lines fix line 81, which was looping for ???? times.
Last edited on
The value of playerturn used for save must be the same value as used for load. As you don't save this value when save and hence aren't reading it from the file, you need to pass the required value to load.

What's the difference between playerturn and turn? Perhaps these changes (not tried):

If L19 is:
 
void savegame(int playerturn)


then L32
 
file << playerturn << endl; 


L50 becomes:
 
int loadgame(int &playerturn, player_information player[3])


L72 is then:

 
 file >> playerturn;

Last edited on
Thanks, I finally did it. I really appreciate your help!
Topic archived. No new replies allowed.