CHAPTER 11
|
Table of Contents
Learning C++:
An Index of Entry Points
2. The A reference document on the basic elements of C++.
3. The Patterns
|
A. ProcessUserChoices Using the top down approach, we
will look at the highest level function that needs to be changed. That
would be 'ProcessUserChoices. B. ReadInstances The function starts by getting the file name from the user and
attempting to instantiate a stream attached to that file. Note that the
algorithm uses an 'if-else' statement while the code only has an 'if'.
That is because the 'exit' statement stops the program. In other words, if
the branch containing the 'exit' instruction is followed, no instructions
after the 'exit' instruction will be executed. The 'else' is provided in
C++ to allow one to have instructions that can be skipped if the code
follows one branch and executed if the code follows another branch. In our
case, if there is a problem connecting the stream to the file, no other
code should be executed but, if the attachment is successful, the code
below the 'if' should always be executed. This is the same idea we
sometimes took advantage of when using the 'return' statement. Once the stream is successfully attached to the input file, the
variables to hold the input values are declared, as is a contract in which
we will place those values before adding the contract to the list of
contracts. We are now ready to execute the loop that will read in the
data. Notice the 'while' loop test condition:
The purpose of this is to use the member function 'eof' (declared in
the root class 'ios' and inherited by 'ifstream') to find out if the "end
of file" symbol has been encountered in the 'inputData' stream. When this
symbol is finally encountered, the system will stop reading data. As a safety precaution, the code to handle invalid data is included
next. One might argue that the user should be responsible for making sure
that the input file is valid, but that is probably asking too much of
users who may not be familiar with the complexities of file management. In
addition, this code is useful during program development, when either the
input code could be wrong, or the code that produces the output may be
writing the file incorrectly. Now we move to the actual input statements. We observed above that
input processing can be very complex. One must pay close attention to how
the data is organized. In the case of any contract database file, the
contract ID field comes first and all fields are separated by commas.
Thus, the code reads in the ID first and then skips past a comma.
As you can see, this instruction says to read up to 81 characters (the
size of our strings) but stop when a comma is read. Thus, a full name is
read in - unless a name includes a comma or is greater than 80 characters!
Since the 'get' function does not read the comma itself, the " inputData
>> chDummy;" line must again be inserted. What follows is a series of lines to read in the each field and by-pass
the commas. After the number of days has been read in, the line:
makes sure the file pointer skips past the end of line marker and is
'pointing' to the beginning of the next contract. Once all the information for a specific contract has been read, the
appropriate member functions are called to put that information into a
contract and, then, that contract is copied to the list of contracts. Note
that this procedure is very similar to the one used in
'InitializeContracts' in the
earlier versions of this program. The only difference is that variable
names are used in the member function calls instead of string and numeric
constants. That finishes the while loop and this function, except for one final
point. Notice that the code assumes that the input file does not end in
the middle of a contract record. In other words, there is no check for
end-of-file after every request for input. It does make the program
susceptible to failure if the input file is corrupt, but, hopefully, any
problem will be gracefully handled by the error checking done at the top
of the while loop. C. OutputData As we observed when designing the file output algorithm, this code borrows
extensively from the code we saw earlier in this chapter to output data
and from the function 'DisplayAllContracts'. Once a stream has been
successfully connected to the output file, the code resets the contract
iterator to 'point to' the first contract in the list and loops through
all the contracts, calling the function 'WriteInfo' on each one. D. WriteInfo
The function 'WriteInfo', in turn, borrows extensively from 'DisplayInfo'.
You can be sure that this code was copied and modified. Why type more
than you have to!?! The complete code for this program is found in the files:
|