(E) Student registerΒΆ

Goal: I will learn to use pointers. I will revise file management and STL containers. I will learn to read code written by another programmer.

Instructions: Retrieve the code template: templates/08/student_register/ -> student/08/student_register/.

Attention

At line 77 in main.cpp given in the code template, the word Erroneuous is written incorrectly. To pass the automatic tests, please, fix it into form Erroneous (remove the extra u).

The code template will be used as a lecture example on this round. In the code template, you can see the commands new and delete, but you need not care about them at this phase. You will learn more about them on the next round.

The program reads the given file including information about students (student number, user id, name, phone number, e-mail, and skype). The program allows printing students in the order according to student numbers as well as in the order according to user ids.

For each of these two orders, there is a map structure of its own in the program. The mapped value of each map is a pointer to the struct that contains the aforementioned data about students.

Your task is to add a new functionality that allows changing the phone number. The new functionality will change the content of the map, but in addition, it is required also to change the content of the file that is read at the beginning of the program execution.

After adding the required functionality, the program will work as follows:

Student file: data.txt
N                  = List ordered by student numbers
U                  = List ordered alphabetically by user ids
C <student_number> = Change the given student's phone number
Q                  = Quit

register> n
123456 ted
Ted Techie
040 123456
ted@tuni.fi
tom_92_

246810 archie
Archie Architect
050 987654
archie@tuni.fi


987654 mike
Mike Mechanic
050 123456
mike@tuni.fi
-Mike-M-

register> u
246810 archie
Archie Architect
050 987654
archie@tuni.fi


987654 mike
Mike Mechanic
050 123456
mike@tuni.fi
-Mike-M-

123456 ted
Ted Techie
040 123456
ted@tuni.fi
tom_92_

register> c 111111
There is no student with the given number!

register> c 123456
Enter a new phone number: 040 77777k

Erroneous phone number: 040 77777k

register> c 123456
Enter a new phone number: 040 777778

register> n
123456 ted
Ted Techie
040 777778
ted@tuni.fi
tom_92_

246810 archie
Archie Architect
050 987654
archie@tuni.fi


987654 mike
Mike Mechanic
050 123456
mike@tuni.fi
-Mike-M-

In the above execution, you can also see the error messages.

You can above see the modified phone number, i.e. it is updated in the map. Because the change command has a student number as its parameter, it is natural to implement the update via the map that is ordered according to student numbers.

Since the aim is to store the changed data permanently, it is not enough to update only the map. Moreover, the modified data must be stored in the input file, which in the above example is data.txt. Write the data to be stored in the file in the order according to user ids.

Finally, the program has the property: If the program is executed again with the same input file, the modified data is there from the beginning, before any other changes.

Tips for completing the assignment:

  • Study the given main program first. It is recommended to draw a picture about the data structure.
  • Recall that referencing to the fields of a data structure depends on the type of the field. Especially, it is different for pointer fields.

A+ presents the exercise submission form here.