home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap13 / password.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  1.6 KB  |  53 lines

  1. /*  password.c -- requires a password to complete the   */
  2. /*                program; illustrates a use of getch() */
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <string.h>
  7. #define GUESS_LIMIT 4
  8. #define WORD_LIMIT 10  /* maximum length of password */
  9. #define TRUE 1
  10. #define FALSE 0
  11. char *Password = "I'mOk";
  12. main()
  13. {
  14.      int g_count = 0;           /* guesses taken */
  15.      int w_count;               /* letters accepted */
  16.      int in_count;              /* letters entered  */
  17.      char entry[WORD_LIMIT + 1];
  18.      char ch;
  19.      int correct, go_on;
  20.  
  21.      do
  22.           {
  23.           puts("Enter the secret password.");
  24.           in_count = w_count = 0;
  25.           /* the following loop accepts no more chars */
  26.           /* than entry[] will hold, but keeps track  */
  27.           /* of total number typed                    */
  28.           while ((ch = getch()) != '\r')
  29.                {
  30.                if (w_count < WORD_LIMIT)
  31.                     entry[w_count++] = ch;
  32.                in_count++;
  33.                }
  34.           entry[w_count] = '\0';
  35.           if (in_count != w_count)
  36.                correct = FALSE;    /* too many chars */
  37.           else
  38.                correct = (strcmp(entry, Password) == 0);
  39.           g_count++;
  40.           go_on = !correct && g_count < GUESS_LIMIT;
  41.           if (go_on)
  42.                puts("\nNo good; try again.");
  43.           } while (go_on);
  44.      if (!correct)
  45.           {
  46.           puts("Sorry, no more guesses.  Bye.");
  47.           return(1);
  48.           }
  49.      puts("Welcome to Swiss bank account 2929100.");
  50.      puts("Your current balance is $10,232,862.61.");
  51.      return(0);
  52. }
  53.