home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / VideoToolboxSources / MultipleChoice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-23  |  1.3 KB  |  51 lines  |  [TEXT/KAHL]

  1. /*
  2. MultipleChoice.c
  3. Makes it easy to ask for keyboard responses to multiple-choice questions,
  4. always providing a default answer, so the user can just hit return.
  5. Here's an example:
  6.     static char bread[]="Bread",fish[]="Fish",*breadFish[]={bread,fish};
  7.     short choice;
  8.     
  9.     printf("Would you like to eat Bread or Fish?");
  10.     choice=MultipleChoice(choice,2,breadFish);
  11.  
  12. HISTORY:
  13. 2/16/93    dgp    wrote YesOrNo().
  14. 5/24/93    dgp    added MultipleChoice().
  15. 9/24/93    dgp    cosmetic.
  16. */
  17. #include "VideoToolbox.h"
  18. #if !defined(__TYPES__)
  19.     typedef unsigned char Boolean;
  20. #endif
  21.  
  22. Boolean YesOrNo(Boolean defaultAnswer)
  23. /* Accept y/n and spell out "Yes" or "No". */
  24. {
  25.     static char no[]="No",yes[]="Yes",*noYes[]={no,yes};
  26.     return MultipleChoice(defaultAnswer,2,noYes);
  27. }
  28.  
  29. int MultipleChoice(short defaultChoice,short n,char *answer[])
  30. /*
  31. Accept one character answer and spell out whole.
  32. Case is ignored. Don't wait for <return>. Looks for first match. 
  33. If no match, then uses defaultChoice.
  34. */
  35. {
  36.     char c;
  37.     short i;
  38.     
  39.     printf(" (%s):",answer[defaultChoice]);
  40.     fflush(stdout);
  41.     do{
  42.         c=getcharUnbuffered();
  43.     }while(c==-1);
  44.     if(c==14)abort();                            /* Crude test for command-period. */
  45.     for(i=0;i<n;i++)if(tolower(c)==tolower((answer[i])[0]))break;
  46.     if(i==n)i=defaultChoice;
  47.     printf("%s.",answer[i]);
  48.     fflush(stdout);
  49.     return i;
  50. }
  51.