Reading from a savegame

Hello all, my problem is:
I'm making a snake game, currently I'm stuck at highscore saving.
I can write to a file named saves.svg, contents are:
1
2
3
name1 12
name2 10
name3 8


now to read from the file I got this:

1
2
3
4
while(myfile>>hsname[i]>>hs[i]&&i<3)
	{
		i++;
	}

the hsname is a string array, that part reads the names. the hs is a int array and it always remains at 0, aparently it doesn't read the integer from file. Is there another option I could use or am I doing something wrong?
You need to show your declarations. What is hs?

Can hsname have spaces?
hs is int array, about hsname, i declared it to be a string array, nothing else.
You must show the code, not just describe the code. It may not be your case, but there have been persons saying "I have that part OK, that's why I don't show it" and it turns out that the part they thought was OK was in fact the one with the problem. So if you are asked to show the declarations, you should show them for clarity's sake. :-)

You also failed to answer the question: Do names in the text file have spaces?
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
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
#include <conio.h>
int hs[3]={0};
string hsname[3];
void HighScores()
{
	HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
	gotoxy(30,9);
	cout<<"HighScores:"<<endl;
	ifstream myfile ("C:\svg.sav");
	int i=0;
	while(myfile>>hsname[i]>>hs[i]&&i<3)
	{
		i++;
	}
	myfile.close();
		gotoxy(30, 11);
		SetConsoleTextAttribute( handle, hred);
		cout<<1<<". "<<hsname[0]<<" "<<hs[0];gotoxy(30, 12);
		cout<<2<<". "<<hsname[1]<<" "<<hs[1];gotoxy(30, 13);
		cout<<3<<". "<<hsname[2]<<" "<<hs[2];
	i=0;
	cin.get();
	if(score>hs[0])
	{
		hs[2]=hs[1];
		hs[1]=hs[0];
		hs[0]=score;
		hsname[2]=hsname[1];
		hsname[1]=hsname[0];
		writescore(0);
	}
	else if(score>hs[1])
	{
		hs[2]=hs[1];
		hs[1]=score;
		hsname[2]=hsname[1];
		writescore(1);
	}
	else if(score>hs[2])
	{
		hs[2]=score;
		writescore(2);
	}

}


Here's the function that uses the reading, gotoxy(); is just the function to move the handle to x and y coordinate.
writescore(); writes it to savegame. That part works(tried it with preset vars).
Last edited on
First, change the string on line 13 to "C:\\svg.sav". As you are using it, the 's' gets escaped. The rest of the code works fine here.
about that...i only got a warning, but now that i fixed it, the problem persists...hs[] is still equal to 0 after reading
Sry for bumping, but my savegame still doesn't work...the int hs[3] is still 0...
I compiled and ran here and it worked. Maybe you are using a wrong or invalid file. Also, check your print function. Try with basic std::cout first.
Last edited on
You continue to neglect the question: Do names in the text file have spaces? Better yet: Copy and paste the contents of the text file here.
I think I've found the problem:when numbers from hs[] are too high they turn to *, = and other symbols...
and yes, there are spaces in the file.

EDIT:
Okay solved that:used std::stringstream for saving.

Is there any way to stop the console pointer from flickering? or turn it off completely?
Last edited on
Topic archived. No new replies allowed.