home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_03 / 1n03016a < prev    next >
Text File  |  1990-07-06  |  5KB  |  201 lines

  1. /* TCATREST.C
  2. ** May 5, 1987
  3. ** Eduard Qualls
  4. ** 
  5. ** --example file for use with TCAT, STCAT, CapBak.
  6. */
  7. #define LINT_ARGS
  8.  
  9. #include "stdio.h" 
  10. #include <ctype.h>
  11.  
  12. #define INPUTERROR     -1
  13. #define INPUTDONE       0
  14. #define MENU_CHOICES   13
  15. #define STD_LEN        79
  16.  
  17. #ifndef TRUE
  18. #define TRUE  1
  19. #define FALSE 0
  20. #define BOOL  int
  21. #endif
  22.  
  23. #define OK      TRUE
  24. #define NOT_OK  FALSE
  25.  
  26. char menu[MENU_CHOICES][STD_LEN] = {
  27.    "SOFTWARE RESEARCH'S RESTAURANT GUIDE \n",
  28.    "\n",
  29.    "      What type of food would you like?\n",
  30.    "\n",
  31.    "      1        American 50s   \n",
  32.    "      2        Chinese    - Hunan Style \n",
  33.    "      3        Chinese    - Seafood Oriented \n",
  34.    "      4        Chinese    - Conventional Style \n",
  35.    "      5        Danish          \n",
  36.    "      6        French          \n",
  37.    "      7        Italian         \n",
  38.    "      8        Japanese        \n",
  39.    "\n\n"
  40. };
  41.  
  42. int char_index;
  43. /* simple program to pick a restaurant, to demo TCAT */
  44. main(argc,argv)  
  45. int    argc;
  46. char    *argv[];
  47. {
  48.    int  i, choice, c,answer;
  49.    char str[STD_LEN];
  50.    BOOL ask, repeat;
  51.  
  52. #ifdef NO_EXT_KEYS
  53. #ifdef LINT_ARGS
  54.    int proc_input();
  55. #else
  56.    int proc_input();
  57. #endif
  58.  
  59. #else
  60.  
  61. #ifdef LINT_ARGS
  62.    int proc_input();
  63. #else
  64.    int proc_input();
  65. #endif
  66. #endif
  67.    c = 3;
  68.    repeat = TRUE;
  69.  
  70.    while(repeat) {
  71.       printf("\n\n\n");
  72.       for(i = 0; i < MENU_CHOICES; i++)
  73.          printf("%s", menu[i]);
  74.  
  75.       gets(str);
  76.       printf("\n");
  77.       while(choice = proc_input(str)) {
  78.          switch(choice) {
  79.             case 1:
  80.                printf("\tFog City Diner          \
  81.                        1300 Battery   982-2000 \n");
  82.                break;
  83.              case 2:
  84.                printf("\tHunan Village Restaurant \
  85.                         839 Kearney   956-7868 \n");
  86.                break;
  87.              case 3: 
  88.                printf("\tOcean Restaurant         \
  89.                         726 Clement   221-3351 \n");
  90.                break;
  91.              case 4:
  92.                printf("\tYet Wah                  \
  93.                         1829 Clement   387-8056 \n");
  94.                break;
  95.              case 5:
  96.                printf("\tEiners Danish Restaurant \
  97.                         1901 Clement   386-9860 \n");
  98.                break;
  99.              case 6: 
  100.                printf("\tChateau Suzanne          \
  101.                         1449 Lombard   771-9326 \n");
  102.                break;
  103.              case 7:
  104.                printf("\tGrifone Ristorante       \
  105.                         1609 Powell    397-8458 \n");
  106.                break;
  107.              case 8:
  108.                printf("\tFlints Barbeque          \
  109.                         4450 Shattuck, Oakland \n");
  110.                break;
  111.              default:
  112.                if(choice != INPUTERROR)
  113.                   printf("\t>>> %d: not a valid choice.\n", 
  114.                           choice);
  115.                break;
  116.          }
  117.       }
  118.       for(ask = TRUE; ask; ) {
  119.          printf("\n\tDo you want to run it again? ");
  120.          while((answer = getchar()) != '\n') {
  121.             switch(answer) {
  122.                case 'Y':
  123.                case 'y':
  124.                   ask = FALSE;
  125.                   char_index = 0;
  126.                   break;
  127.                case 'N':
  128.                case 'n':
  129.                   ask = FALSE;
  130.                   repeat = FALSE;
  131.                   break;
  132.                default:
  133.                   break;
  134.             }
  135.          } 
  136.       }
  137.    }
  138. }
  139.  
  140.  
  141. int proc_input(in_str)
  142. char *in_str;
  143. {
  144.    int tempresult = 0;
  145.    char bad_str[80], *bad_input;
  146.    BOOL got_first = FALSE;
  147.    bad_input = bad_str;
  148.  
  149.    while(isspace(in_str[char_index]))
  150.       char_index++;
  151.    for( ; char_index <= strlen(in_str); char_index++) {
  152.       switch(in_str[char_index]) {
  153.          case '0':
  154.          case '1':
  155.          case '2':
  156.          case '3':
  157.          case '4':
  158.          case '5':
  159.          case '6':
  160.          case '7':
  161.          case '8':
  162.          case '9':
  163.             /* process choice */
  164.             tempresult = tempresult * 10 + 
  165.                          (in_str[char_index] - '0');
  166.             got_first = TRUE;
  167.             break;
  168.  
  169.          default:
  170.             if(chk_char(in_str[char_index])) {
  171.                return(tempresult);
  172.             }
  173.             else {
  174.                if(char_index > 0 && got_first)
  175.                   char_index--;
  176.                while(char_index <= strlen(in_str)) {
  177.                   if(chk_char(in_str[char_index]))
  178.                      break;
  179.                   else
  180.                      *bad_input++ = in_str[char_index];
  181.                   char_index++;
  182.                }
  183.                *bad_input = '\0';
  184.                printf("\t>>> bad input: %s\n", bad_str);
  185.                char_index++;
  186.                return(INPUTERROR);
  187.             }
  188.       }
  189.    }
  190.    return(INPUTDONE);
  191. }
  192.  
  193. BOOL chk_char(ch)
  194. char ch;
  195. {
  196.    if(isspace(ch) || ch == '\0')
  197.       return(OK);
  198.    else
  199.       return(NOT_OK);
  200. }
  201.