Sams Teach Yourself C in 21 Days

(singke) #1
The functionality of these routines should be obvious. The add_hour()function simply
adds to the hours value in the structure, the add_minute()adds to the minutes data mem-
ber, and the add_second()adds to the seconds data member. Listing B3.2 illustrates the
use of these three simple functions with the startandendtime structures.

LISTINGB3.2 time2.cpp. Using the add...functions with the time structure
1: // Program using a time structure with additional functions
2: // for adding time.
3: //
4: #include <iostream.h>
5:
6: struct time {
7: int hours;
8: int minutes;
9: int seconds;
10: };
11:
12: void print_time(struct time tm);
13: void add_hour(struct time *tm);
14: void add_minute(struct time *tm);
15: void add_second(struct time *tm);
16:
17: int main(int argc, char* argv[])
18: {
19: struct time start_time = {7, 15, 20};
20: struct time end_time = {10, 20, 30};
21:
22: // Print initial times
23: cout << “\nStart time: “;
24: print_time(start_time);
25: cout << “\nEnd time: “;
26: print_time(end_time);
27:
28: // Add 1 hour, 1 minute, and 1 second to end time
29: add_hour(&end_time);
30: add_minute(&end_time);
31: add_second(&end_time);
32:
33: // Print final times
34: cout << “\n\nStart time: “;
35: print_time(start_time);
36: cout << “\nNew end time: “;
37: print_time(end_time);
38:
39: return 0;
40: }
41:
42: // Print a time structure in h:m:s format

668 Bonus Day 3

38 448201x-Bonus3 8/13/02 11:19 AM Page 668

Free download pdf