OCR Worked Example – Development

//OCR Worked Example – Development
OCR Worked Example – Development 2016-10-25T17:28:33+00:00


This section deals with developing solutions for the identified file handling task needed by the project.  You should start by planning as much as you can.  As you do this you might need to do some coding to help you check that you can achieve the desired result but don’t, under any circumstances go straight into trying to program your solution – it won’t work.

Searching for an entry in the HighScore file

Search for entry

This is fundamental routine that will be needed by other parts of your solution.

This is a basic process that will be used by several other aspects of the project so needs to be considered first.

As I have decided to use a CSV format file and Small Basic reads whole lines rather than discrete values, I can actually search for either a players name or their high score in the same process using the Text object to see if their name or their score is in the line I am examining.  All my code needs to do is read each line of the file, checking for a match and if there is one, return the line number that I have just read.  With this information I can carry out a number of other actions on that specific line in the file.  If the data is not in the file, I need to either return a message or something that indicates that the data has not been found.  One possible solution would be to return zero if not found because Small Basic files count lines from one.

To do this I need only one item of data – either the name of the player or their high score.

Splitting a file line into its CSV values

As my program will be reading lines of CSV data it is going to be essential to be able to convert a line into its CSV values.  I will provide a function to do this, taking a line of CSV text as a parameter and returning an array of the values extracted.

As there are only two values in any line, all I need to do is search for the position of the comma and split the line around this point.  Small Basic has there methods that will enable me to do this: Text.IndexOf(), Text.GetSubText() and Text.GetSubTexToEnd().

Splitting CSV

Another important routine – you will frequently need to break apart the CSV line of data.

Updating a players high score

To do this I will need to be able to search for a player by name.  This is already planned and will return the line number in the file containing the required data.  If this routine returns a zero, the required player is not in the file and the process will abort.

With the required line in the file, I can access both the player name and their high score so my routine can take both name and high score, make them into a CSV formatted line of text and simply write it to the file at the line  specified.  If I don’t send the players name to this routine, it can easily extract the required data using a routine already planed and then combine th items together to write the data back.  This is one of the many advantages of direct file access  which Small Basic provides.

Deleting a players entry


I need to write every line except the one being deleted.

This part of the program will need to search for the players name in the data file using the above routines.  If the name is found, (line is between 1 and 10), the name and score need to be removed from the file.  This can be done in two ways – a blank line can be written to the file or the file can be re-written leaving out the entry to be deleted.  The latter is the better way to deal with this aspect of the task as it makes sure that the file doesn’t end up with blank lines.

To do the file needs to be read and written to a temporary file leaving out the line of the player to be deleted.  Once complete the original file should be deleted and the temporary file renamed as the master file.

Adding a new player and record


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:

  1. When there are less than 10 high scores already in the table, and
  2. 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…

  1. Find the line number of the lowest score
  2. 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.

Displaying High Scores

Although this feature is not an explicit requirement by the task, it makes sense to provide it.  This will enable the data can be viewed after changes have been made to it.  This enables users to verify that the system works as expected.

It simply requires each line of the file to be read – already designed and then to display each line suitable formatted.  This will require the lines to be split into their separate values – again already done, and then output in a suitable tabular format.

This tabular format can be achieved within a loop using cursor control methods to place each element in a column.  Headings can be provided, again using cursor methods to align correctly.

The User Interface

All sections of the program will need to make careful use of the display area available.  At the end of each there will need to be a method of returning to the main menu.  This must wait for user input so that any output can be seen before the menu is redisplayed.  Each action should create its own screen, optionally clearing the menu and displaying a suitable title and appropriate layout.  Routines that will result in many lines of output should clear the screen so that no scrolling takes place.  Colours will be used to highlight and emphasise the option chosen and output relating to that choice.