Assignment 5

Write a system that tracks the top five scores for a game. The system has a list of five items, and each item has a name and the corresponding score. Initially, the list consists of five items with the name Anonymous and the score of 0. The system supports four commands: add, del, show, exit.

  • The command show displays the name and the score of the top five players in the descending order of scores.
  • The command exit terminates the system.
  • The command add takes a score and a name from the user. The score entered by the user is assumed to be between 1 and 99. If the score is greater than any of the top five socres, then it should be added to the list and the lowest score discarded. Otherwise, the list should remain unchanged.
  • The command del takes an item number from the user. The item number is assumed to be between 1 and 5. The item in the list corresponding to the item number entered by the user will be removed from the list, and the name Anonymous and the score of 0 will be added to the end of the list.

A complete example is given below (illustration).

Enter commands (add, del, show, exit):
add 50 Potter
add 20 Ron
add 45 Granger
show
The top five scores are:
   1.       Potter, Score: 50
   2.      Granger, Score: 45
   3.          Ron, Score: 20
   4.    Anonymous, Score: 0
   5.    Anonymous, Score: 0
add 30 Malfoy
show
The top five scores are:
   1.       Potter, Score: 50
   2.      Granger, Score: 45
   3.       Malfoy, Score: 30
   4.          Ron, Score: 20
   5.    Anonymous, Score: 0
del 1
show
The top five scores are:
   1.      Granger, Score: 45
   2.       Malfoy, Score: 30
   3.          Ron, Score: 20
   4.    Anonymous, Score: 0
   5.    Anonymous, Score: 0
exit 
  • Your solution should be based on the code template.
  • You probably need to add 8 lines to implement the member function newScore(), and add 7 lines to implement the member function removeItem().
  • Input-output samples:
  • Suggestion:
    • Using member function at() instead of brackets [] for accessing vector elements, because at() will catch the error when your vector subscripts are out of range.