hey,
For the last few days I've been working on my first game which is a simple arcade game and im basically finished but iv just got to add a very simple highscore system.
I've managed to get the code to write to the highscore file:
ofstream file("highscore.txt");
file << score;
But I cant find out how I would load what i've saved into the variable highscore.
please help:)
You just do the opposite. Open a file in output( I think) mode, then file >> score or whatever other variable you're using.
Well, without outputting spaces, that won't go over well. But basically, yeah.
could you give me the code to do that?
thanks:)
I tried this:
string line;
ifstream file ("highscore.txt");
if (file.is_open())
{
while (! file.eof() )
{
getline (file,line);
textprintf_ex( buffer, font, 300, 300, makecol(255, 255, 255),-1, "Highscore: %d", line);
}
file.close();
}
It compiles fine but when I run it I get
Super Jet Pack Boy.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
This probably isn't the source of the crash, but in the textprintf_ex() call, you tell it to expect an integer, and then pass a string.
string line; ifstream file ("highscore.txt"); if (file.is_open()) { while (! file.eof() ) { getline (file,line); textprintf_ex( buffer, font, 300, 300, makecol(255, 255, 255),-1, "Highscore: %s", line.c_str()); } file.close(); }
Thanks Dustin Dettmer it worked =D
To be fair you should be thanking LennyLen, as we pretty much said the same thing.
thanks LennyLen too, sorry I did'nt see your other reply.
Don't use that in an Allegro program.
line is already declared by Allegro.
This is the code I give my students to handle high scores.
http://www.timorg.net/high_score.zip
If you are interested but have problems, post here or msg me.
@Timorg: It's amazing how similar your code is to mine
Just wondering: Why are you using so many c-isms in a c++ code? And if this code is intended to be an example, I'd suggest to avoid magic numbers in your code and either #define or const the appropriate numerals.
Why would you ever use const?
Just wondering...
Why would you ever use const?
Maybe to indicate a constant? It also makes your code more easy to read / maintain.
Or do you want to know when to use const instead of a define?
Yeah.
I don't see the advantage, surely define is faster since it's done by the preprocessor...?
Well, there shouldn't be a speed difference at all. The main reason to use const is that it is known at compile time. So you'll see the constants in error messages or while debugging.
Also, you can avoid some pitfalls using const:
#define FOO 10 /* oups... forgot to add brackets here, but who cares */ #define BAR FOO+1 #define BAZ 2* BAR /* BAZ is now: 2 * 10 +1 = 21 * instead of 2*BAR (= 2*11) */
You can avoid the problem if you place all your defines in () but this can be forgotten.
It's also considered the better "C++" style.
Defines won't give an error (except for random screwed up ones) if you have a naming conflict whereas consts will.
IMO naming conflicts is the main reason to not use macros. Naming conflicts were sent here by the devil (just think of winalleg.h X[)
@Timorg: It's amazing how similar your code is to mine
Just wondering: Why are you using so many c-isms in a c++ code? And if this code is intended to be an example, I'd suggest to avoid magic numbers in your code and either #define or const the appropriate numerals
Our code being similar just means that it is being done in the right way.
I wrote that original code back in 2000 for a class assignment, It didn't have any error checking, it was just the basic code, and I shared it with the other people in the subject, 3 people got in trouble for plagarism, because they didn't site my code where everyone else did.
As for the C-isms, I guess I am a C programer at heart, but the code was for visual studio 6, and combining C and C++ code is a pain, so its basically C code in a cpp file. I only really use C++ for classes and the stl, everything else is done in C. I also don't really understand iostream for binary files, its just quicker and simpler for me to use the C functions to do it. And stl sorts arnt as simple as the qsort function.
As for magic numbers, yeah I should have used constants for the length, cause I remember old amiga games with scrolling highscores that go up from 100, and to change that you need to change values in a few places, where there should just be a const in the header.