CSE 143
Homework #3
Hints, tips, corrections, and announcements
The most recent announcements will be listed first.
4/21/99 Some clarifications.
- Inserting an appointment into an appointment list keeps the list
in order. This means that other elements may be moved when an appointment is
inserted.
- List positions are numbered from 1 to n, where n is the size of the
list. Note that this is different from array numbering, which goes from 0 to
n-1. See how AppointmentList::index() is used for details.
- The functions in appfile.h return false if an error or end of file
is reached. There is also an error parameter that is passed by reference,
which is set only for errors (if your program handles errors in the input data).
Thus when you reach the end of the file, your functions should return false
but should NOT set the error parameter true.
- Note that loadData should only return false if there is an error
reading the data file, since the code in main() exits if loadData returns
false. Remember that you should loop like the demo program does if the user
types in a bad filename.
- An appointment name is delimited with the " character--that is,
it starts with the character after the first " in the line, and it continues
right up to (but not including) the next " character. Note that this may
include spaces, etc.
4/20/99 Some tips on processing files and streams.
- First, make sure you understand everything in HW2 (socuser.cpp) where a file is opened
and read in.
- The demo program is fairly robust; it can detect and recover from a variety of errors in
the file. Your program doesn't have to (this time). The only error you have to
handle (according to the detailed instructions) is if the user types a bad filename.
Otherwise, if there is bad data on the file, and your program blows up, that's OK.
- The data file we gave you as a sample is complicated. It is often effective to get
your program running first on simple data. For example, can your program process a
file that has no appointments, just a today's date line? If you can already do this, how
about the next step: a file which contains regular (one-time only) appointments, etc. etc.
- In addition to the file stream functions mentioned in lecture, the file stream class
have a number of other methods. Take a look at Appendix A. Some that are
mentioned there that you might find helpful at some point include (for an input stream F):
F.get; F.peek; and F.ignore.
- Sometimes (not always) there are simple ways to do complex things. For example,
one student told me he was going to read in a date such as 4/19 by reading characters into
a string, writing a loop to look for the '/' characters, then converting the numeric
characters to integers. Yes, you can make that work, if you can't think of
a simpler way.
4/20/99 What do the functions called from main do?
Now that you have had some time to think about the assignment, here are a few hints on
the functions called by main.
upToDate should go through the list of appointments, and remove all the ones before
today (incrementing and adding back in any repeating ones!), so that the resulting list
only has appointments with valid dates that occur today or later. It takes as
arguments three streams (remember, pass by reference!) so that it can interact with the
user if necessary. The sample solution from which the demo was built only uses the
error stream (to see its use, try making an appointment file that has no appointments with
good dates, or that has only appointments that occur before the "today" date
given on the first line). The function takes two extra stream arguments because you
might want to handle such cases better than we do (of course, you don't have to do so!).
processToday should go through all the appointments that occur today, remove them from
the list (again, increment them and add them back in if they repeat), and print out their
names. Try to match the behavior of the demo.
storeFile is pretty straightforward; the only caveat is that the date written to the
output file should be tomorrow's date (can you see why?) instead of today's date.
Since today is passed as an argument, storeFile will have to do something to get the date
of tomorrow.
4/18/99 Passing streams as parameters.
Streams in C++ are just variables, so there's no reason you can't pass them as
parameters to functions. Make sure you have the type right (istream or ostream).
The one catch is that they should be passed as reference parameters. Our old
friends cout (and cerr, if you are using that) are ostreams, and cin
is an istream, so they could also be passed as parameters. For example, the upToDate
function you see called from main might have a prototype like this: void upToDate(AppointmentList& list, Date now, istream&
in, ostream& err, ostream& out);
Why do this? It allows you to easily change where the
streams are coming from or going to, without changing the function. One day you pass
cout as the parameter for out, and another day, for debugging purposes,
you might pass the name of an output file stream that you created in main. Or you
could call such the function twice in the program and have the streams going different
places each time.