Trouble when create and load save file

So, my assignment is to create a horseboard game, and I am struggling with save/load part.

I have 2 problem, the first one is that after I finish the save part and go to the txt file, there is my data but also many irrelevent like a bunch of 0 and other strings. And when I load it, it some how read the wrong information. I try to see where the bug is my assign the true value instead of what it read, but it go into a loop or some thing and the return a mess value

I will be very greateful if you guys can help me, I have been struggling for weeks


I will post the struct, save and load part for short because I think it is where the problem stay.

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  #include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include<windows.h>
#include<bits/stdc++.h>

using namespace std;

string line;
// Field information //
struct field_information
{
	char mode;
	int fieldsize, cellblock, cellreturn, maxran, minran;
	int cellblocklocate[10];
	int cellreturnlocate[10];
};	

// Player information //
struct player_information
{
	char playername;
	int horsenum;
	int pos[4];
	int victorytime;
};

field_information field;

player_information player[4];
						
int col, row;
int lastcol[4][4];
int lastrow[4][4];


// savegame //
void savegame(field_information &field, player_information,int numberofmatches,int num,int playertogo)
{
	ofstream loaddata;
    loaddata.open("C:\\Users\\Public\\Documents\\loaddata.txt");
 
	 
    loaddata << field.fieldsize << endl; 
    loaddata << field.mode << endl; 
    
    loaddata << field.cellblock << endl; 
    for (int i = 1; i <= field.cellblock; i++)
    	loaddata << field.cellblocklocate[i] << endl; 
    loaddata << field.cellreturn << endl; 
    for (int i = 1; i <= field.cellreturn; i++)
	    loaddata << field.cellreturnlocate[i] << endl; 
    loaddata << numberofmatches << endl; 
    loaddata << field.maxran << endl; 
    loaddata << field.minran << endl; 
    loaddata << num << endl; 
    
    loaddata << playertogo << endl;
    
    for (int i = 1; i <= num; i++)
    	loaddata << player[i].playername << endl;
    	
	for (int i = 1; i <= num; i++)
		loaddata << player[i].horsenum << endl;
		
	for (int i = 1; i <= num; i++)
		for (int j = 1; i <= player[i].horsenum; j++)
			loaddata << player[i].pos[j] << endl;
			
	
	
	for (int i = 1; i <= num; i++)
		loaddata << player[i].victorytime;			
							 
    loaddata.close();  
    exit();
}	  


int loadgame(field_information &field, player_information (&player)[4], int &numberofmatches,int &playernum,int &playertogo)
{
	ifstream file("C:\\Users\\Public\\Documents\\loaddata.txt");
	if (file.is_open())
	{
		getline(file, line);
		if (atoi(line.c_str()) != 0) 
	    {   
			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)
        {
			
			field.fieldsize = atoi(line.c_str());
			
        	file >> field.mode;
        		 
			file >> field.cellblock; 
			
			for (int i = 1; i <= field.cellblock; i++)
			{
    			file >> field.cellblocklocate[i];
    		}	

			file >> field.cellreturn;
			
			for (int i = 1; i <= field.cellreturn; i++)
			{
    			file >> field.cellreturnlocate[i];
			}
	 
			file >> numberofmatches;
			
			file >> field.maxran;
		 
			file >> field.minran;
			
			file >> playernum;
			
			for (int i = 1; i <= playernum; i++)
			{
    			file >> player[i].playername;
			} 
			
			
			for (int i = 1; i <= playernum; i++)
			{
				getline(file, line);
				cout << line;
    			player[i].horsenum = atoi(line.c_str());
			} 
			
			for (int i = 1; i <= playernum; i++)
				for (int j = 1; i <= player[i].horsenum; j++)
				{
    				file >> player[i].pos[j];
				} 	
				
			file >> playertogo;
			
			for (int i = 1; i <= playernum; i++)
			{
				file >> player[i].victorytime;
			} 

			file.close();  
    		return 1;		
        }
        else 
        {
        	file.close();  
    		return 0;
		}		
}			
int main ()
{

	
	// Ready for statictical outcome //
	


	
	int numberofmatches;
	numberofmatches = 1;
	
	// Start the game //
	
	
	int situation;
	srand (time(NULL));
		
		// Welcome message //
	cout << "Welcome to Horse Boardgame" << endl;
		
		// Menu //
		
	int gameload = loadgame(field, player, numberofmatches, playernum, playertogo);

}   


And here what inside txt file
3
n
1
23
1
13
1
2
1
1
d
1
1
2
0

Last edited on
pretend we don't know what a horseboard is.
also, what, exactly, SHOULD be in the file? It looks like you have a good start, but its unclear what it should be doing, which makes it hard to tell you what its doing wrong and where.

<ctime> <cstdlib> are better, they are the C++ (namespace enabled) versions of these C includes.
try to avoid global variables. for now, leave it until its debugged, but I highly recommend not using them in the future.

is the file you listed correct, or the broken one you said you are having trouble to save?
if it is correct, is that what the saved files need to look like?
L132 is likely to be a problem. Mixing >> with getline() can cause a problem. >> ignores leading white-space but doesn't remove the trailing white-space char that terminated the extraction. getline() doesn't ignore leading white-space but removes the terminating char. You might want to try for L132

 
getline(file >> ws, line);


Also instead of using atoi() with .c_str() you can use std::stoi(). See http://www.cplusplus.com/reference/string/ for the various options.
@jonnin
The file I list is what it is supposed to be, the file I originally save by this program having the same part but adding many "0" line

I post the file like it suppose to because I want you to correct me if anything wrong with loading the data
@seeplus
I have tried using only >> or getline() but it keep bugging.

When I use getline(), L102 to L114 cannot read the right information so I change to >> but when I do it, L132 make me go into some looping thing and cannot continue so I try mixing.

Also, I tried your solution, adding "ws" but it's not working, and go into infinity loop


Well now's a good time to get to grips with the debugger. Use the debugger to trace through the code and watch the contents of the variables - especially the file read code. When you find that execution is not as expected, then you're found a problem. Fix that - and repeat until the program works as expected.

For future reference, it's suggested that a program is developed in small parts, with each part compiling OK and tested OK before moving on to the next part. Then you don't get a whole program that doesn't work.
Topic archived. No new replies allowed.