(E) Counting points

Goal: I will go through file reading once more. I will learn to use the data structure map.

Instructions: Create a new project: student/05/points/.

Implement a program that helps count the players’ points in a game. It should be possible to insert the amount of points into the program in any order, and it should not matter to the program how many points each player gets. The only actions of the program are to add up the points of a single player, and printing the final points of all players, organized alphabetically by the players’ names.

After startup, the program asks for the name of the file. If the file cannot be opened, the program prints the error message ”Error! The file <filename> cannot be opened.”, where <filename> is replaced with the name of the file. Then, the program terminates with the return value EXIT_FAILURE.

First, the program prints the headline ”Final scores:”, and after that, the names of the players, each on their own line. The contents of one line should be the player’s name, colon and space, and then the total of the players’ points. After that, the program exits with the return value EXIT_SUCCESS.

As an example of how the program should work, consider the following case. If the contents of the file example1.input are:

essi:5
pietari:9
essi:2
pietari:10
pietari:7
aps:25
essi:1

then the program will work as follows:

Input file: example1.input
Final scores:
aps: 25
essi: 8
pietari: 26

If the file not_a_file.txt does not exist, the program will work like this:

Input file: not_a_file.txt
Error! The file not_a_file.txt cannot be opened.

Tips for completing the assignment:

  • Remember to change the execution folder of your program so that the program can find the text files you are using. You can read about how to do this in the assignment at Line numbering a file.

A+ presents the exercise submission form here.