Sams Teach Yourself C in 21 Days

(singke) #1
In Review 383

Day 11, “Implementing Structures, Unions, and TypeDefs.” This program groups all the
data for each person into a structure named record. Much of this data should look famil-
iar; however, a few new items are being tracked. Lines 25–27 contain three arrays, or
strings, of characters to hold the first name, last name, and phone number. Notice that
each of these strings is declared with a +1in its array size. You should remember from
Day 10, “Working with Characters and Strings,” that this extra spot holds the character
that signifies the end of a string.
This program demonstrates proper use of variable scope (see Day 12, “Understanding
Variable Scope”). Lines 34 and 36 contain two global variables. Line 36 uses an int
calledlast_entryto hold the number of people who have been entered. This is similar
to the variable ctrused in Week 1 in Review. The other global variable is list[MAX],an
array of record structures. Local variables are used in each of the functions throughout
the program. Of special note are the variables month_total,grand_total,x, andyon
lines 157–159 in display_report(). In Week 1 in Review, these were global variables.
Because these variables apply only to display_report(), they are better placed as local
variables.
An additional program control statement, the switchstatement (see Day 13, “Advanced
Program Control”), is used on lines 70 through 81. Using a switchstatement instead of
several if...elsestatements makes the code easier to follow. Lines 72 through 79 exe-
cute various tasks based on a menu choice. Notice that the defaultstatement is also
included in case you enter a value that isn’t a valid menu option.
Looking at the get_data()function, notice that there are some additional changes from
Week 1 in Review. Lines 104 and 105 prompt for a string. Line 105 uses the gets()
function (see Day 14, “Working with the Screen, Printer, and Keyboard”) to accept a per-
son’s first name. The gets()function gets a string and places the value in
list[last_entry].fname. You should remember from Day 11 that this places the first
name into fname, a member of the structure list.
display_report()has been modified; it uses fprintf()instead of printf()to display
the information. The reason for this change is simple. If you want the report to go to the
printer instead of the screen, on each fprintf()statement, change stdouttostdprn.
Day 14 covered fprintf(),stdout, andstdprn. Remember that stdoutandstdprnare
streams that output to the screen and the printer, respectively.
continue_function(), on lines 194 to 219, has also been modified. You now respond to
the question with YorNinstead of 0 or 1. This is more user-friendly. Also notice that the
clear_kb()function from Listing 13.9 has been added on line 213 to remove any extra
characters that the user entered. Additionally, the fflush()function is used to clear any
characters that might have been left in the buffer.

23 448201x-W2R 8/13/02 11:12 AM Page 383

Free download pdf