Sams Teach Yourself C in 21 Days

(singke) #1
382 Week 2

CH 12 196: int ch;
197:
198: printf(“\n\nDo you wish to continue? (Y)es/(N)o: “);
199:
CH 14 200: fflush(stdin);
CH 14 201: ch = getchar();
202:
203: while( ch != ‘n’ && ch != ‘N’ && ch != ‘y’ && ch != ‘Y’ )
204: {
205: printf(“\n%c is invalid!”, ch);
206: printf(“\n\nPlease enter \’N\’ to Quit or \’Y\’ to Continue: “);
207:
CH 14 208: fflush(stdin); /* clear keyboard buffer (stdin) */
209: ch = getchar();
210: }
211:
212:
213: clear_kb(); /* this function is similar to fflush(stdin) */
214:
215: if(ch == ‘n’ || ch == ‘N’)
216: return(NO);
217: else
218: return(YES);
219: }
220:
221: /*--------------------------------------------------------------------*
222: * Function: clear_kb() *
223: * Purpose: This function clears the keyboard of extra characters. *
224: * Returns: Nothing *
225: * Note: This function could be replaced by fflush(stdin); *
226: *--------------------------------------------------------------------*/
227: void clear_kb(void)
228: {
CH 09 229: char junk[80];
CH 14 230: gets(junk);
231: }

LISTINGR2.1 continued

It appears that, as you learn about C, your programs grow larger. Although this
program resembles the one presented after your first week of programming in C,
it changes a few of the tasks and adds a few more. Like Week 1’s review, you can enter
up to 100 sets of information. The data entered is information about people. You should
notice that this program can display the report while you enter information. With the
other program, you couldn’t print the report until you had finished entering the data.
You should also notice the addition of a structure used to save the data. The structure is
defined on lines 24–32. Structures often are used to group similar data, as discussed on

ANALYSIS

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

Free download pdf