Sams Teach Yourself C in 21 Days

(singke) #1
172 Week 1

you whether you want to continue or quit. On lines 67–71, you can see that this function
uses the printf()function from Day 7, “Fundamentals of Reading and Writing
Information,” to display the instructions.
On lines 157 to 172,continue_function()uses some of the features covered at the end
of the week. This function asks whether you want to continue (line 159). Using the
whilecontrol statement from Day 6, “Basic Program Control,” the function verifies that
the answer entered was a 0 or a 1. As long as the answer isn’t one of these two values,
the function keeps prompting for a response. After the program receives an appropriate
answer, an if...elsestatement (covered on Day 4, “Statements, Expressions, and
Operators”) returns a constant variable of either YESorNO.
The heart of this program lies in two functions:get_data()anddisplay_report(). The
get_data()function prompts you to enter data, placing the information into the arrays
declared near the beginning of the program. Using a forstatement on line 89, you are
prompted to enter data until contis not equal to the defined constant YES(returned from
continue_function()) or until the counter,ctr, is greater than or equal to the maxi-
mum number of array elements,MAX. The program checks each piece of information
entered to ensure that it is appropriate. For example, lines 94–98 prompt you to enter a
month. The only values that the program accepts are 0 to 12. If you enter a number
greater than 12 , the program again prompts for the month. Line 115 calls
continue_function()to check whether you want to continue adding data.
When the user responds to the continue function with a 0 , or the maximum number of
sets of information is entered (MAXsets), the program returns to line 50 in main(), where
it calls display_report(). The display_report()function, on lines 119 to 149, prints
a report to the screen. This report uses a nested forloop to total incomes for each month
and give a grand total for all the months. This report might seem complicated; if so,
review Day 6 for coverage of nested statements. Many of the reports you will create as a
programmer are more complicated than this one.

This program uses what you learned during your first week of teaching yourself C. There
was a large amount of material to cover in just one week, but you did it! If you use
everything you learned this week, you can write your own programs in C. However,
there are still limits to what you can do.

12 448201x-W1R 8/13/02 11:15 AM Page 172

Free download pdf