i just want to ask
| valrecx (1) | |||
| I am beginner in C++, I just want to ask, I don't know how to do an automatic saved program. Example i made a hangman with a score then when i exit the game and open it again i want to still see that high score. Something like that. Please help thnks | |||
| Duoas (1381) | |||||||
The usual method is to keep a little data file (a simple text file would do nicely) that lists the top n high-scores and player names and whatever else you want. An example file (with five high-scores) might look something like this:
In your code, every time a game ends, update your list of high-scores (that you have in memory) and write them to the high-scores file (on disk). When the game starts, initialize your list of high-scores to zero, no-name, etc., then look for the file on disk and, if it is there, read them into the list. Hope this helps. Oh yeah, almost forgot: When reading strings from a text file, use getline(), not >>. So to read a score might be:
This example is completely devoid of error checking just to make things simple. Here it is with error checking:
The sizeof/sizeof trick is to handle whatever the size of the array you create without allowing overflow. Hope this helps. | |||||||
This topic is archived - New replies not allowed.
