View Full Version : C++ question
The_Doctor
02-13-2007, 17:26
This should be quite easy for you mighty C++ veterans.:yes:
How do I create a highscores table in a game, so that I can view it and add to it?
I have a feeling it involves it looking in a .txt file and making changes to it.
Thanks:2thumbsup:
Sasaki Kojiro
02-13-2007, 17:31
I'm thinking file I/O. Search it on google, it's pretty simple.
The_Doctor
02-13-2007, 17:42
I found it, it does indeed look simple.
Thanks:2thumbsup:
Definitely file I/O. Do you want the file to be human-readable (and easily modified) outside the game? Or do you want the uninitiated to not be able to decipher it from, say, notepad?
The_Doctor
03-22-2007, 16:53
Things have gone a bit wrong, I asked my lecturer how to get the numbers from the text file (I don't think he understood what I was trying to do), he used the getline() code to get the numbers out of the file. The problem is that they are stored in a char array, which means each digit on the number is in a different part of the array and finding out which number is bigger will be very difficult.
The program (which will be intergrated into the game) works like this:
1. Open the file.
2. Get the numbers from file.
3. Input a number that called CS (Current score, this would normally come from the game).
4. A big nested if statement to find out where the CS number should go in the list, or if it is too small display a message telling the player they do not have a high score.
5. Display the updated high scores.
6. Update the file with the new scores.
7. Close the file
Question:
1. Should I be using a text file for this program?
2. Is there any other way of getting the numbers out of the file?
3. Is there a way to convert a char array into an int?
Thanks.:2thumbsup:
The_Doctor
03-22-2007, 20:39
I think I have solved the problem by using the atoi() thing.
The_Doctor
03-22-2007, 21:21
I have got it all working now.:yes:
doc_bean
03-22-2007, 21:26
How did you do it ? I've been messing around with the getline() thing too, but it's not really convienent for loading in tables or matrices.
Blodrast
03-23-2007, 00:03
Use strtol() instead of atoi(). atoi() doesn't detect, or return, certain kind of errors. strtol() does, AND it has more functionality (bases, etc).
And it has its variants, as well, strtod(), strtoll(), strtoul()...
Kommodus
03-23-2007, 03:45
Ultimately, I think you'll want to use something other than a text file for this application. A text file will do the job, but as you get more programming knowledge you'll be able to use:
1. A binary file containing records (I think this is what most games do)
2. A database of some kind, such as MySQL
I haven't used C++ in a while (mainly C# these days), so I won't try to explain either, but don't worry: it'll come. Neither is all that hard.
The_Doctor
03-23-2007, 20:30
How did you do it ?
The numbers in the text file where like this:
1|2|3|4|5|6|7|8|9|10
To extract a number I used:
HighScoresfile.getline(buf1, 1024, "|");
buf1 was a char array with 1024 places.
Then I copied the contents of the array into an int called HS1:
HS1 = atoi(buf1);
Then I repeated the it, only with the buf and HS number increasing until it got to 10.
I have feeling that this is not the best way to do it, but we have not been taught how to do stuff with files yet. It works, so I am pleased with myself.
Ultimately, I think you'll want to use something other than a text file for this application. A text file will do the job, but as you get more programming knowledge you'll be able to use:
1. A binary file containing records (I think this is what most games do)
2. A database of some kind, such as MySQL
We will probably be doing this stuff next year.
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.