Add new data deals with both scenarios – less than 10 records and a score higher than the existing lowest.
There are two situations the a new entry needs to made in the file. These are:
- When there are less than 10 high scores already in the table, and
- When the current score is higher than the LOWEST score in the table.
In the first instance, the process is straight forward as a name and high score line of CSV text can simply be appended to the end of the file.
If an existing high score needs to be overwritten then it needs to be the LOWEST score in the table. In this case, I need to…
- Find the line number of the lowest score
- Then overwrite it with a new line of text formatted as CSVs.
Finding the lowest score requires that I iterate through the table checking the score against a higher score. If the score found is lower than the current Low score then the current low score needs to be replaced and the line number recorded.
Routine to find the lowest score in the table.
When found, I need both the line number in the file and the low score found. I ca then check this against the score I have been asked to record. Only if this new score is higher than the lowest score found in the table, should I proceed and replace the data.