Objectives
- Gain experience working with structures
- Learn how to use structures with functions
- Learn how to use structures with arrays
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
Open the file
structures.h
in emacs. This program contains the definition of aTime
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
Define a new structure called
Appointment
. This should have 3 fields: astart
time, anend
time (bothTime
datatypes) and adescription
(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 theTime
structure.
In
main
, declare two variables of typeTime
. Prompt the user, then callread_time
to get times for your 2 variables.Complete the function
write_time
to write out a single time in the formatHH:MM
. Call this frommain
to make sure it works.Write a function called
read_appt
to read in all the data for an appointment (useread_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 thatcin.getline
will read and discard the the newline character, butcin >> variable
will only skip over leading whitespace. You may need to usecin >> ws
orcin.ignore
to clear the buffer, particularly when you are reading things repeatedly.Modify
main
to prompt, then read all the data for an appointment. You’ll also need to declare a variable of typeAppointment
.
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 mainlabs
directory and runmake lab=structures
. Note that the test depends on theAppointment
structure being defined exactly as specified in step 2.
- 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 calledappts.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 toread_appt
andread_time
inmain
to make sure you’re reading to the write place.
- 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.