Hints on error detection and reporting
As you might have noticed, the error reporting of
Letter::read(...) and Font::read(...)
in Homework 2 was rather crude and from from a stylistic point of view.
If we are reading a letter and we get an error,
the Letter::read(...) function prints out
what happened. The specification file, lmatrix.h;
however, said nothing about
Letter::read(...) printing out to
cout. In fact, by having Letter::read(...) print to
cout we have limited our Letter class to be
used with console applications that are highly user interactive. If instead
we wanted to write a Windows App or an application that in batch processed
many letter files, we would
not be able to use this Letter class because it is tied to cout.
Specification for Letter::read(...)
Reads in a letter from either a stream or a file and on error, sets a
instance variable on the current letter indicating where the error occurred
and then it returns false. On a successful read, true is returned.
The client that calls Letter::read(...) can
then recover exactly which error occurred and
notify the user if desirable (in our case,
the client can cout an error message).
Specification for Font::read(...)
Reads in the font file and returns true if the read was completely
successful. Otherwise it sets some instance variables to indicate which
errors occurred and returns false.
It continues to try to make the best of
the font file regardless of whether it has had an error previously. For
example, if your font file has three letters and the second letter file has
an error then this font should have two valid letters stored in it and this
font should be usable, but false
should be returned to indicate to the
client that errors occurred and that it should check the this Font
object for what errors occurred.
Note that main.cpp will have to be modified to use font
even if
font.read(...) fails and
to print out how font.read() failed before prompting for
words to spell.
The Goal
Ultimately, the code can be rewritten and
encapsulated in such a way that Letter::read(...)
and Font::read(...) do no error reporting to cout, but
keep error information on the Letter and Font class
instance respectively so that the client can generate an error report that is
as verbose as
your current
error report is. The client only need to
check this error information if the read returns
false.