|
-
Dec 30th, 2002, 07:24 AM
#9
Thread Starter
Fanatic Member
Why should I use classes? I've never needed them.
Here is what I came up with for sorting and saving the highscore list. I'm sure the sorting is very inefficient but it gets the job done. If there is something I can change to make it faster please tell me. As for the saving; it doesn't really make it hard to edit or anything... Guess I'll have to come up with something like a very simple encryption or something...
Code:
void SortHS(HighScore HS[], int n)
{
int iHighest = 0;
int iNum = 0;
int iCounted = 0;
HighScore temp;
for(int j = 0; j < n; j++)
{
for(int i = iCounted; i < n; i++)
{
if(HS[i].Score > iHighest)
{
iHighest = HS[i].Score;
iNum = i;
}
}
temp = HS[iCounted];
HS[iCounted] = HS[iNum];
HS[iNum] = temp;
iCounted++;
iHighest = 0;
}
}
void main()
{
ifstream fin ("score.bin", ios::in | ios::binary);
char buffer[3];
for(int n = 0; n < 10; n++)
{
fin.read(HS[n].Name, sizeof(HS[n].Name));
fin.read(buffer, sizeof(buffer));
HS[n].Score = atoi(buffer);
}
SortHS(HS, 10);
for(n = 0; n < 10; n++)
cout << HS[n].Name << " " << HS[n].Score << endl;
ofstream fout ("score.bin", ios::out | ios::trunc | ios::binary);
for(n = 0; n < 10; n++)
{
fout.write(HS[n].Name, sizeof(HS[n].Name));
itoa(HS[n].Score, buffer, 10);
fout.write(buffer, sizeof(buffer));
}
fout.close();
return;
}
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|