Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12.     FILE* data = fopen("data","rw");
  13.     FILE* libbooks = fopen("books","r");
  14.     int n;
  15.     int o;
  16.     char books [100];
  17.     char bookname [30];
  18.     char password [10];
  19.     char username [10];
  20.     char eof;
  21.     int bob;
  22.     char exiter;
  23.     char password2 [10];
  24.     char username2 [10];
  25.     char eof2;
  26.     char loginstatus [20];
  27.     int userstatus = 0;
  28.     int available;
  29.     int copies;
  30.     printf("WELCOME\n--------------------------\n\n");
  31.     while (1)
  32.     {
  33.         printf("Please choose what to do: \n\n");
  34.         printf("1- Login. \n 2- Scan for a book. \n 3- Request a book.\n 4- Exit.\n ");
  35.  
  36.         scanf("%d",&bob);
  37.         switch (bob)
  38.         {
  39.         case 1:
  40.             printf("Enter Username:\n");
  41.             scanf("%s",username);
  42.             printf("Enter Password:\n");
  43.             scanf("%s",password);
  44.  
  45.             while (fscanf(data,"%c",&eof) != EOF)
  46.             {
  47.                 fscanf(data,"%s %s",username2,password2);
  48.                 if( ( strcmp(password,password2) && strcmp(username,username2) ) == 0)
  49.                 {
  50.                     printf("login accepted!\n\n");
  51.                     userstatus = 1;
  52.                     break;
  53.                 }
  54.                
  55.                
  56.             }
  57.            
  58.             if(userstatus == 0)
  59.                 printf("Authentication failed!\n\n");
  60.        
  61.             break;
  62.  
  63.         case 2:
  64.  
  65.             printf("Enter the title of the book you want:\n");
  66.             scanf("%s",bookname);
  67.  
  68.             while(fscanf(libbooks,"%c",&eof2) != EOF)
  69.             {
  70.  
  71.                 fscanf(libbooks,"%s %d %d",books,&available,&copies);
  72.                 if (strcmp( bookname,books) == 0)
  73.                 {
  74.  
  75.                     printf("Book found!\n");
  76.                     if(available ==0 && copies == 0)
  77.                     {
  78.                         printf("The book is not available right know.\n");
  79.  
  80.                     }
  81.                    
  82.                     else if (available != 0 && copies != 0)
  83.                     {
  84.                         printf("The book is available,there are %d copies of the book.\n",copies);
  85.                     }
  86.                 }
  87.  
  88.             }
  89.  
  90.             break;
  91.         case 4:
  92.             return 0;
  93.             break;
  94.            
  95.             case 5:
  96.             if(userstatus == 1)
  97.                 printf("status: logged in\n\n");
  98.            
  99.             else
  100.                 printf("status: logged out\n\n");
  101.                
  102.             break; 
  103.            
  104.            
  105.  
  106.  
  107.         default:
  108.             printf("Enter a number between the options listed above please.\n");
  109.  
  110.         }
  111.  
  112.         fclose(data);
  113.         fclose(libbooks);
  114.  
  115.     }
  116.  
  117.     return 0;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement