PR2EN3: Structures

Lab materials

In the lab, a sample solution will be written together and commented where data fill be read from the file and stored in a struct array. This is also published as a code example for those not participating in the labReading from a file into structure array

Lab tasks

In this lab, you have one task which is expanded by 4 advanced tasks.

Lab task: employee search

In this program, we will mimic an employee database from where we will filter out employees based on search criteria.

Data file

Download the test data from the following link https://blue.pri.ee/ttu/files/iax0584/andmefailid/3_data_short.zip

Data file is composed of randomly generated data.

Requirements

  • Read employee data from the file
    • File has one  record per line
    • Every data field is separated by a space. All data fields are single word.
    • Data structure in the file:
      <eID> <first_name> <last_name> <city>
  • You need to keep the data in the memory as a struct array
  • Program must work in a situation where the exact number of records is unknown and can change (e.g. somebody gets employed, fired or leaves)
    • Create a reasonable limit in your program and test that you wouldn’t go beyond it
    • If the data file is longer than the allowed limit, print a corresponding message. Behavior after this is up to you.
  • Allow the operator to perform searches based on the city name
    • Program will output all employees in the city
    • Show how many matches were found (E.g. [0 / 91])
    • Search must be repeatable without exiting and restarting the program
    • In the base task, print all the data available – eID, last name, first name, city
  • User must be able to exit the program when they desire. Create functionality for that (e.g. a specific command)
  • Reading the data file more than once within your program is not permitted.
  • The structure members don’t need to be an exact match for the file. You can add additional struct members – this is useful for the advanced tasks.

Example output

NB! The example output already includes all 4 advanced tasks!

Click me to see the output

Advanced task 1: Sorting structures

Requirements:

  • The list of employees must be sorted alphabetically by the last name in ascending order.
  • Output name format must be  last name, first name

Advanced task 2: List available cities

Requirements

  • List all the cities present in the file
  • List of the cities must be generated based on the data file (must not be hardcoded into the program)
  • Operator must be able to print the list of city whenever they wish by typing the appropriate command. Add a command for this to your program.
  • Operator must be able to repeatedly print out the list during the runtime of the program.
  • The list of the cities can only be composed once per execution of the program.

Advanced task 3: Better search

Requirements

  • The search must be case-insensitive. Searching for either  TALLINN  or tallinn  must print all employees living in Tallinn.
  • Search must support partial matches. E.g. when searching for  all , you should display employees living both in Kallaste and Tallinn.
  • Both features must working at the same time. The last result should also be obtainable by entering ALL or aLL

Advanced task 4: Parse the eID

Requirements

  • Do not display the eID in the output of your program
  • Display the employees date of birth with the format d. MMMM yyyy

Official information about eID: https://www.riigiteataja.ee/akt/114022017005

Short description of it in English: https://en.wikipedia.org/wiki/National_identification_number#Estonia

Click me to see date and time format guide

m – Minutes from 0 to 59.
mm – Minutes from 00 to 59. Minutes from 0 to 9 are prefixed with a zero.

h – Hour from 1 – 12.
hh – Hour from 01 to 12. Hours from 1 to 9 are prefixed with a zero.
H – Hour from 0 – 23
HH – Hour from 00 – 23. Hours from 0 to 9 are prefixed with a zero.

d – Day from 1 to 31
dd – Day from 01 to 31. Days from 1 to 9 are prefixed with a zero.
ddd – Day as a short name (Mon, Tue, Wed, …)
dddd – Day as a long name (Monday, Tuesday, Wednesday, …)

M – Month from 1 to 12
MM – Month from 01 to 12. Months from 1 – 9 are prefixed with a zero.
MMM – Month as a short name (Jan, Feb, Mar, Apr, …)
MMMM – Month as a full name (January, February, March, …)

y – Year from 0 to 99
yy – Year from 00 to 99
yyy – Year with a minimum of 3 digits (1 -> 001; 15 -> 015; 145 -> 145; 1949 -> 1949)
yyyy – Year with 4 digits

After the class, you should

  • Know how to declare a new structure type
  • Know how to pick members for a structure
  • Know what is structure padding and how it affects the size of the structure
  • Know what affects the final size of a structure
  • Know which structure member access exist and be able to use the dot operator
  • Know how to create an array of structures and read data from a file into that array of structures
  • Know how to assign structures
  • Know how to give existing types new names (use typedef)

Additional content