954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Highscores problem

Ok so a friend of mine came up with this code to create a highscores list for a game when i run it, it works perfect you can see everything fine. the problem comes when it tries to reopen the file again. The file has abunch of gibberish inside and doesnt work any ideas?

ive played with it a little but cant seem to find the problem.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>

struct highscore {
char name[50];
int score;
};

main()
{
int i,j,tempscore,tempno;
char empty[]={"empty"};
ifstream fin("highscore.txt");
ofstream fout("highscore.txt");
highscore h[11];
j=0;
fin.seekg(0);
while (fin) {
fin.read((char*)&h[j],sizeof(highscore));
j++;
}
if (j==0) {
for (i=0;i<10;i++) {
strcpy(h[i].name,empty);
h[i].score=0;
}
}
fin.close();
cout<<"enter the name\n";
cin.getline(h[10].name,50);
cout<<"enter the score\n";
cin>>h[10].score;
tempscore=0;
for (i=0;i<10;i++) {
tempscore=0;
for (j=i;j<11;j++) {
if (h[j].score>=tempscore) {
tempscore=h[j].score;
tempno=j;
}
}
char name[50];
int p;
p=h[i].score;
h[i].score=h[tempno].score;
h[tempno].score=p;
strcpy(name,h[i].name);
strcpy(h[i].name,h[tempno].name);
strcpy(h[tempno].name,name);

}
fout.seekp(0);
for (i=0;i<10;i++)
{
fout.write((char*)&h[i],sizeof(highscore));
cout<<h[i].name<<"\t\t\t\t"<<h[i].score<<"\n";
}
fout.close();
getch();
}
mpike
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Can't follow the code with the lack of formatting. Please format it and repost.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: