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.hin emacs. This program contains the definition of aTimetype (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: astarttime, anendtime (bothTimedatatypes) 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
Appointmentneeds to be defined first so the program will compile, even though the next two steps only rely on theTimestructure.
In
main, declare two variables of typeTime. Prompt the user, then callread_timeto get times for your 2 variables.Complete the function
write_timeto write out a single time in the formatHH:MM. Call this frommainto make sure it works.Write a function called
read_apptto read in all the data for an appointment (useread_timewhere 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:30Hint: be very careful with your
cinbuffer! Remember thatcin.getlinewill read and discard the the newline character, butcin >> variablewill only skip over leading whitespace. You may need to usecin >> wsorcin.ignoreto clear the buffer, particularly when you are reading things repeatedly.Modify
mainto prompt, then read all the data for an appointment. You’ll also need to declare a variable of typeAppointment.
Complete the function called
write_apptto print out the details of an appointment in the format:10:00 - 10:30: Coffee breakcalling
write_timewhere necessary.Call this from
mainto be sure it works. This is the tested function for this lab; when you’re satisfied that it works,cdup to the mainlabsdirectory and runmake lab=structures. Note that the test depends on theAppointmentstructure 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_apptsand 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.txtis provided with appointment data that can be used with input file redirection.
Note: when using input file redirection, your previous calls to
cinwill use file instead of waiting for user input. Comment out the previous calls toread_apptandread_timeinmainto 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.