Hardware Reference
In-Depth Information
The fragment of C++ code that follows is an example of the Do ... Loop
While logical construct:
do
{
print_label(code);
cout << "Label printed\n";
cout << "Enter Y to print another label, any other key to
exit: ";
cin = response;
} while(response = = 'y' || response = = 'Y')
cout << "Shutting down ... please wait!\n";
Loop structures are explained in detail in Chapter 6.
Error checking and input validation
Error checks and input validation routines should be incorporated whenever data
is input and before the system accepts the data for processing. Error handling
routines should be incorporated to warn the user that a fault has occurred and
indicate from which source the error has arisen. This caveat also applies to
operator input; an unacceptable input should be echoed to the user together
with the range of acceptable responses. Care should be exercised when inputs
are defaulted. The default response should result in inactivity rather than any
form of positive action on the part of the system. Furthermore, the program
should demand confirmation where a response or input condition will produce
an irreversible outcome.
Event-driven programs
With an event-driven program the processor must share its time with the main
process and any sub-processes responding, for example, to a user clicking
on a button, an input from a sensor, or a timer signalling the end of a time
period. Since these events may occur at any time and in any sequence this
requires a somewhat different approach than that which would be appropriate
for a strictly sequential process. The important thing to remember is that events
can take different forms and they can occur virtually at any time. This makes it
difficult, or even impossible, to describe the program by means of a conventional
flowchart; the program must be able to make an effective response to an event
whenever it occurs.
As an example of an event-driven process consider the work of a receptionist
in a small but busy office. The main process associated with the job can simply
be described as 'receptionist'. The sub-processes might then be 'telephone
answering', 'greeting visitors', 'dealing with general enquiries', 'opening and
sorting incoming mail', 'preparing outgoing mail', and so on. Within the main
process events can occur in any order. For example, the telephone might ring, a
visitor might arrive, the mail might be delivered, and so on. It might be difficult,
or even impossible, to say when these events will occur in any particular working
day. However, they will occur and each of the sub-processes must be handled
correctly for the main process to be satisfied.
Search WWH ::




Custom Search