Advertisement
ppupil2

assignment B_input

Apr 24th, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef enum {false, true} bool;
  5.  
  6. bool dupcheck_code(char a[11], int k) { /* check duplicate a in array bkinstck[].code which have k elements */
  7.     int i;
  8.     for (i = 0; i<k; i++) {
  9.         if (strcmp(bkinstck[i].code, a) == 0) {
  10.             return(true);
  11.         }
  12.     }
  13.     return(false);
  14. }
  15.  
  16. void input_code(int i) { /* input bkinstck[i].code */
  17.     char a[11];
  18.    
  19.     while (1) {
  20.         printf(" Enter code (enter STOP to stop): ");
  21.         fflush(stdin);
  22.         gets(a);
  23.         //scanf("%[^\n]", a);
  24.         if ((strlen(a) > 0) && (strlen(a) <= 10)) {
  25.             if (dupcheck_code(a, i) == true) {
  26.                 printf(" The code %s already exists, please re-enter!\n", a);
  27.             }
  28.             else {
  29.                 strcpy(bkinstck[i].code, a);
  30.                 break;
  31.             }
  32.         }
  33.         else {
  34.             printf(" Invalid code, please re-enter!\n");
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement