Lab 11: Structures

Feb 13, 2024  β”‚  m. Feb 17, 2024 by Charlotte Curtis

Objectives

Setup

Before starting on this lab, double check your git configuration. Run the following:

$ git status

You should see something like:

On branch main
Your branch is ahead of 'teacher/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

where the specific number of commits may vary.

If this is the case, you’re good to do the usual git pull to fetch the new lab starter code.

If instead you see something like:

On branch main
Your branch and 'teacher/main' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

you may need to run git pull --no-rebase instead of the usual git pull to make sure you don’t end up in another dangling commit situation.

Finally, cd into the structures directory to begin the lab.

Exercises

  1. Open the file structures.h in emacs. This program contains the definition of a Time type (using a 24-hour clock), plus function declarations to:

    • read and write a time
    • read and write an appointment
    • read in a series of appointments
  2. Define a new structure called Appointment. This should have 3 fields: a start time, an end time (both Time datatypes) and a description (a C-string of length 64). Make sure to declare these fields in the order given, as the tests are using curly brace initialization and will give unexpected results if the order is different.

Note: originally this was step 4 in this set of instructions, but Appointment needs to be defined first so the program will compile, even though the next two steps only rely on the Time structure.

  1. In main, declare two variables of type Time. Prompt the user, then call read_time to get times for your 2 variables.

  2. Complete the function write_time to write out a single time in the format HH:MM. Call this from main to make sure it works.

  3. Write a function called read_appt to read in all the data for an appointment (use read_time where necessary). There should be no prompts in the function, as later on you will use this to read an appointment from a file. Sample data for one appointment is:

    Coffee break
    10:00
    10:30
    

    Hint: be very careful with your cin buffer! Remember that cin.getline will read and discard the the newline character, but cin >> variable will only skip over leading whitespace. You may need to use cin >> ws or cin.ignore to clear the buffer, particularly when you are reading things repeatedly.

  4. Modify main to prompt, then read all the data for an appointment. You’ll also need to declare a variable of type Appointment.

  1. Complete the function called write_appt to print out the details of an appointment in the format:

    10:00 - 10:30: Coffee break
    

    calling write_time where necessary.

    Call this from main to be sure it works. This is the tested function for this lab; when you’re satisfied that it works, cd up to the main labs directory and run make lab=structures. Note that the test depends on the Appointment structure being defined exactly as specified in step 2.

  1. Finally, set up an array that will hold up to 10 appointments. Uncomment the function read_daily_appts and call it. This reads in one day’s appointments and stores them in an array. It also returns the number of appointments. A file called appts.txt is provided with appointment data that can be used with input file redirection.

Note: when using input file redirection, your previous calls to cin will use file instead of waiting for user input. Comment out the previous calls to read_appt and read_time in main to make sure you’re reading to the write place.

  1. Write code (in main) to determine which appointment has the latest start time, and prints out its details.

Assessment

To build and run the test for this lab, cd to your top-level labs directory and run the command:

$ make lab=structures

When you are satisfied with your write_appt function, add and commit your changes to your labs repo, then git push to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.



Previous: Lab 10: C-Strings
Next: Lab 12: Files and Command Line Arguments, plus separate compilation