Files aren't saving and loading correctly?

So in the program that I've been asking quite a bit about on this forum for help with, I have it so that the user can save and load their game. Usually, the game saves and loads correctly, but sometimes, I'm not sure when it happens, but the game either saves or loads the file incorrectly. I'll be using Dual Swords as a weapon, save my game, and when I load it, it either says I'm not using a weapon, or that some potion is now my weapon. Does anyone have any idea why it'd do this? I can post code if you'd like.
Code please, or a link to it. This sounds like an interesting bug.
Haha code for the whole game? Or just the save and loading functions? If you want the whole game, I'll send you a link to download it, if you want just the save and load functions I can post the code here
We would probably have to see the save/load functions as well as probably the class definitions at least.

Dual Swords

FTW
Last edited on
Okay, well here's the save and load functions

Save:
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
int save()
{
	int invsav = 0;
	int invwep = 0;
	int invarmr = 0;
	int invmisc = 0;
	int invchk = 0;
	ofstream myfile;
	myfile.open ("savefile.nae");
	myfile << Cstats.name << '\n';
	myfile << Cstats.Class << '\n';
	myfile << Cstats.race << '\n';
	myfile << str << '\n';
	myfile << dex << '\n';
	myfile << con << '\n';
	myfile << Int << '\n';
	myfile << wis << '\n';
	myfile << cha << '\n';
	myfile << inventory[0].ArmorClass << '\n';
	myfile << Cstats.BaseAttk << '\n';
	myfile << Cstats.Health << '\n';
	while(invsav!=1)
	{      // Saves weapons
		while(invwep!=1)
		{
			myfile << inventory[invchk].weapon << '\n';
			myfile << inventory[invchk].invamt << '\n';
			myfile << inventory[invchk].invno << '\n';
			invchk++;
			if(inventory[invchk].weapon=="")
			{
				myfile << '\n';
				invwep = 1;
			}
		}
		invchk = 0;
                // Saves armor
		while(invarmr!=1)
		{
			myfile << inventory[invchk].armor << '\n';
			myfile << inventory[invchk].invamt << '\n';
			myfile << inventory[invchk].invno << '\n';
			invchk++;
			if(inventory[invchk].armor=="")
			{
				myfile << '\n';
				invarmr = 1;
			}
		}
		invchk = 0;
                // Saves misc items
		while(invmisc!=1)
		{
			myfile << inventory[invchk].miscitems << '\n';
			myfile << inventory[invchk].invamt << '\n';
			myfile << inventory[invchk].invno << '\n';
			invchk++;
			if(inventory[invchk].miscitems=="")
			{
				myfile << '\n';
				invmisc = 1;
			}
		}
		invsav = 1;
	} 
	myfile << Cstats.gold << '\n';
	myfile << Cstats.progress << '\n';
	myfile << Cstats.currentweapon << '\n';
	myfile << Cstats.currentarmor << '\n';
	myfile << Cstats.clevel << '\n';
	myfile << Cstats.Exp << '\n';
	myfile << reqxp << '\n';
	myfile << Cstats.MaxHealth << '\n';
	myfile << Cstats.MaxMp << '\n';
	myfile << Cstats.Mp << '\n';
	myfile.close();
	cout << "File saved successfully." << endl;
	return 0;
}


Load:
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
185
186
187
188
int load()
{
	int loadinv = 0;
	int loadwep = 0;
	int loadchk = 0;
	string invamt;
	string goldt;
	string str1;
	string dex1;
	string con1;
	string Int1;
	string wis1;
	string cha1;
	string temp;
	string health;
	string armorclass;
	string attk;
	int loadarmr = 0;
	int loadmisc = 0;
	ifstream myfile ("savefile.nae");
	if(myfile.is_open())
	{
		getline(myfile,Cstats.name);

		getline(myfile,Cstats.Class);

		getline(myfile,Cstats.race);

		getline(myfile,str1);
		stringstream convert(str1);
		convert >> str;

		getline(myfile,dex1);
		stringstream convert1(dex1);
		convert1 >> dex;

		getline(myfile,con1);
		stringstream convert2(con1);
		convert2 >> con;

		getline(myfile,Int1);
		stringstream convert3(Int1);
		convert3 >> Int;

		getline(myfile,wis1);
		stringstream convert4(wis1);
		convert4 >> wis;

		getline(myfile,cha1);
		stringstream convert5(cha1);
		convert5 >> cha;

		getline(myfile,armorclass);
		stringstream convert7(armorclass);
		convert7 >> inventory[0].ArmorClass;

		getline(myfile,attk);
		stringstream convert8(attk);
		convert8 >> Cstats.BaseAttk;

		getline(myfile,health);
		stringstream convert9(health);
		convert9 >> Cstats.Health;

		while(loadinv!=1)
		{	// Loads weapons
			while(loadchk!=1)
			{
				getline(myfile,temp);

				if(temp=="")
				{	
					loadchk = 1;
				}
				else
				{
				
				bool weapon=false;
				if(weapon==false)
				{
					Cstats.currentweapon = temp;
					weapon=true;
				}
				inventory[loadwep].weapon = temp;
				getline(myfile,invamt);
				stringstream convert(invamt);
				convert >> inventory[loadwep].invamt;
				getline(myfile,inventory[loadwep].invno);
				loadwep++;
				}
			}
			loadchk = 0;
                        // Loads armor
			while(loadchk!=1)
			{
				getline(myfile,temp);

				if(temp=="")
				{
					loadchk = 1;
				}
				else
				{
				inventory[loadarmr].armor = temp;
				getline(myfile,invamt);
				stringstream convert(invamt);
				convert >> inventory[loadarmr].invamt;
				getline(myfile,inventory[loadarmr].invno);
				loadarmr++;
				}
			}
			loadchk = 0;
                        // Loads Misc. Items
			while(loadchk!=1)
			{
				getline(myfile,temp);
				if(temp=="")
				{		
					loadchk = 1;
				}
				else
				{
				inventory[loadmisc].miscitems = temp;
				getline(myfile,invamt);
				stringstream convert19(invamt);
				convert19 >> inventory[loadmisc].invamt;
				getline(myfile,inventory[loadmisc].invno);
				loadmisc++;
				}
			}
			loadinv = 1;
		}
	
		getline(myfile,goldt);
		stringstream convert6(goldt);
		convert6 >> Cstats.gold;

		getline(myfile,Cstats.progress);

		getline(myfile,Cstats.currentweapon);

		getline(myfile,Cstats.currentarmor);

		string slvl;
		string sExp;
		string sreq;
		string smheal;
		string smp;
		string smmp;

		getline(myfile,slvl);
		stringstream convert12(slvl);
		convert12 >> Cstats.clevel;

		getline(myfile,sExp);
		stringstream convert13(sExp);
		convert13 >> Cstats.Exp;

		getline(myfile,sreq);
		stringstream convert14(sreq);
		convert14 >> reqxp;

		getline(myfile,smheal);
		stringstream convert15(smheal);
		convert15 >> Cstats.MaxHealth;

		getline(myfile,smmp);
		stringstream convert16(smmp);
		convert16 >> Cstats.MaxMp;

		getline(myfile,smp);
		stringstream convert17(smp);
		convert17 >> Cstats.Mp;

		myfile.close();
		cout << "File loaded successfully." << endl;
		gamemenu();
		system("pause");
	}
	else
	{
		cout << "Error: File either damaged, or does not exist. Returning to main menu." << endl;
		Sleep(2000);
		system("cls");
		main();
	}
	return 0;
}


And here's where the variables are defined:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Character{
	string name;
	string weapon;
	string armor;
	string Class;
	string race;
	string miscitems;
	string currentweapon;
	string currentarmor;
	int Health;
	int invamt;
	int BaseAttk;
	int MaxHealth;
	int ArmorClass;
	int Exp;
	int clevel;
	int MaxMp;
	int Mp;
	string invno;
	string progress;
	int gold;
} inventory[28], Cstats;


Any other variables referenced to in the save or load functions should be just int's (str, dex, con, Int, wis, cha)
Anyone have any ideas?
Do you know what a Class is? It would help you clean this up SOO MUCH!.

I'm going to take a shot in the dark and say that this is an issue parsing the data. Try using a prefix when you save data something like:
In Your Save File
WEAPON:DUAL_SWORDS
ARMOR:POTATO_SACK


Psuedo-Code
1
2
3
4
5
6
InFile.getline(&FileData, 100, ':');
//code code code
if(FileData == "WEAPON")
{InFile.getline(&Character.weapon, 100);}
if(FileData == "ARMOR")
{InFile.getline(&Character.armor, 100);}


This way you don't have to worry about what order your data is saved in, you Load File does the data sorting for you.

EDIT: Also having now re-read your origional post, you can use "typedef" to define a custom data type like a weapon, then only except that datatype for your weapon slot. Just a thought.
Last edited on
Uhm, no I do not haha. But I can go check out the tutorial on this site if you think that'd be a good idea. ...which I'm assuming it is, since you said it would clean it up so much.

And for your psuedo-code, I'm not sure I understand how it would check and only get whats after "WEAPON" or "ARMOR". And what is "FileData", and "InFile"? I'm thinking InFile is just the file name, and FileData is just a variable. But I could be wrong.

And alright, I'll have to try that out. Thanks!
Last edited on
So I tried what you told me to do with the save file, and I'm getting an error where I have it get the line. Underneath the period it says
9 IntelliSense: no instance of overloaded function "std::basic_ifstream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list


Here's how I wrote it, so maybe you'll see something wrong with it.

1
2
3
4
5
6
7
8
9
10
ifstream myfile ("savefile.nae", ifstream::in);
string FileData;
		while(loadinv!=1)
		{	
			myfile.getline(&FileData,100,':');
			if(FileData=="WEAPON")
			{

			}
		}
That's probably because I'm really bad at this pseudo-code thing. Drop the '&' if you haven't already, and FileData should be a char array.

There's actually a bunch of stuff wrong with the code I gave you. It wasn't supposed to be a plug and play as much as an illustration of an idea.

EDIT: A more functional way would be this:
Data From File

WEAPON SWORD


1
2
3
4
    iFile >> FromFile;
    
    if(FromFile == "WEAPON")
    {iFile >> weap;}


This way won't let you use white space in the name right from the file, but there are ways around that
Last edited on
Ah! I got it! Thanks so much!
Topic archived. No new replies allowed.