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 three extra tasks.

Lab task [W03-1]: employee search

The purpose of this task is to introduce you to using structures. The task will mimic a very basic employee database where the user can search for employees and that also supports commands.

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
    • Reading the data file more than once within your program is not permitted.
    • 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>
  • The data needs to be kept 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
    • If the data file is longer than the allowed limit, print a corresponding message. Behavior after this is up to you.
  • The program must allow for repeated searches and processing of commands without restarting the application
  • 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
  • There must be support for commands
    • Commands should start with a “:” to identify them from city searches
    • Command “:all” must print all people in the database, without any filtering
    • Command “:exit” must close the program

Hints

The command parser sounds “scary”, while the solution is extremely simple. All of this you learned in Programming 1 in the strings class. This is a 3-step process

  1. Identify that user entered a command (check that the 0-th character is ‘:’) – this was an example on the slide
  2. Make a pointer to the second character ( char *command = input + 1; )-  this is pointer arithmetic from week 1, also used in week 2 for the file extension
  3. Compare the command to known commands

Example output

The first example is about command handling. If it cannot recognize a command, it states so. If a command is entered without a colon, it will search instead (default behavior).

The second example shows how searching is handled.

Note that the output of “:list” command is not shown due to it being somewhat long.

The last example shows the program when all extra tasks have been completed. This is wrapped in a spoiler to reduce the size on normal vieweing.

Click me to see the complete output

Extra task 1 [W03-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 (cannot be hardcoded into the program)
  • List of cities must be ordered alphabetically
  • Add a command “:list” to your command parser.
  • 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

Extra task 2 [W03-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.
    NB! Notice that :all  is a command printing everyone and all  is a search phrase printing people in Tallinn and Kallaste.

Extra task 3 [W03-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