Advertisement
Guest User

test.c

a guest
Dec 24th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.74 KB | None | 0 0
  1. /**
  2. THIS CODE IS DESIGNED TO RUN ON UNIX SYSTEMS (MAC OR LINUX)
  3. IF YOU ARE USING WINDOWS TO COMPLIE THIS CODE, YOU'LL NEED TO MAKE A COUPLE CANGES.
  4.  
  5. THESE CHANGES ARE:
  6. 1. REPLACE <unistd.h> WITH <time.h>
  7. 2. REPLACE <termios.h> WITH <conio.h>
  8. 3. REMOVE THE GETCH FUNCTION FROM THE CODE (IT IS INDECATED IN THE CODE AS "GETCH FUNCTION")
  9. 4. REPLACE ALL INSTANCES OF sleep() WITH Sleep()
  10. 5. IN THE SLEEP FUNCTION, THE DIGIT WHICH INDICATES THE NUMBER OF SECONDS TO PAUSE FOR SHOULD BE IN MILLISECONDS.
  11.    I.E. 1 SECOND = 1000 MILLISECONDS
  12. 6. SEE COMMENTS FOR MORE CHANGES.. (LOGIN FUNCTION)
  13. **/
  14.  
  15. //--- Header Files ----
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>  /**This library is only for unix users (Mac or Linux) for windows users this library would have to be
  20.                      replaced with the time.h library to use the sleep() function**/
  21.  
  22. #include <termios.h> /**This library is only for unix users (Mac or Linux) for windows users this library would have to be
  23.                      replaced with the conio.h library to use the getch() function**/
  24. //---------------------
  25.  
  26. //--- Function Proto-types-----
  27. void login();               //Login Function username: Admin password: P@$$w0rd (case sensitive)
  28. int displaymenu();          //Display the menu
  29. /**void menu();             //Calls the respective function based on the option selected in the menu displayed
  30. void add();                 //Allows the user to add patients into a structure array
  31. void search();              //ALlows the user to search for a patients based on patient number
  32. void display(); **/         //Displays all patient detals
  33. //---------------------------------------------------------------------------------------------------
  34.  
  35. //------------------------- MAIN FUNCTION ------------------------
  36. int main()
  37. {
  38.     int choice;
  39.     login();                //Calling the login function
  40.     choice=displaymenu();   //Calling the display menu function and storing the return valued the the variable "choice"
  41.     //menu(choice);         //Calling the "choice" option function from the menu function
  42.     getch();                //Holds the information on the screen until a key is pressed
  43.     return 0;
  44. }
  45. //--------------------------------------------------------------------------------------------------
  46.  
  47. //---------------- DISPLAY MENU FUNCTION -----------------
  48. int displaymenu()
  49. {
  50.     int option;
  51.     puts("\n \t \t  |=======================================|");
  52.     puts("\t \t  | \t           MAIN MENU \t \t  |");
  53.     puts("\t \t--|=======================================|--");
  54.     printf("\t \t|  Select the number that corresponds with  |\n \t \t|\t \t your option.\t \t    |\n");
  55.     puts("\t \t|-------------------------------------------|");
  56.     printf("\t \t|\t  1. Add Patient \t\t    |\n");
  57.     printf("\t \t|\t  2. Look up Patient \t \t    |\n");
  58.     printf("\t \t|\t  3. Display Patient Details\t    |\n");
  59.     printf("\t \t|\t  4. Exit\t \t \t    |\n");
  60.     puts("\t \t|-------------------------------------------|");
  61.     printf("\t \t| Option: ");
  62.     scanf("%d",&option);
  63.     puts("\t \t|-------------------------------------------|");
  64.     return option;  //Returns the number the user chooses and stores it in the "choice" variable in the main function
  65. }
  66. //--------------------------------------------------------------------------------------
  67.  
  68. //------------------- LOGIN FUNCTION -----------------------------
  69. void login()
  70. {
  71.     int i;                                       //Variable Declaration
  72.     char uid[25],pwd[25],s_uid[25]={"Admin"};    /**Created three variables one for user ID (uid[25]) and one user password
  73.                                                  (pwd[25]). s_uid[25] is the varible holding the user ID the user is suppose to
  74.                                                  enter to get in the system. Anything other that user ID (Admin) will be not
  75.                                                  have access to the system**/
  76.  
  77.     char s_pwd[25]={"p@$$w0rd"},ch=' ';          /**Created a variable (s_pwd[25]) to hold the password the user is suppose to
  78.                                                  enter to get access to the system. Anything other that user password (p@$$w0rd)
  79.                                                  will be not have access to the system. **/
  80.     puts("\n\n\n\n\n\t\t\t\t    WELCOME");
  81.     puts("\n\t\t    CHAPELTON DENTISTRY REGISTRATION SYSTEM");
  82.     puts("\n\t \t \t |-----------------------------|");
  83.     puts("\t \t \t |             LOGIN           |");
  84.     puts("\t \t \t |-----------------------------|");
  85.     printf(" \t \t \t |Enter the User ID : ");
  86.     scanf("%s",uid);
  87.     getchar();
  88.     printf(" \t \t \t |Enter the Password : ");
  89.     i=0;
  90.     while(1)     //Loops code forever
  91.     {
  92.         ch=getch();
  93.         if(ch==10)           //10 is the ASCII value for the "Enter" key. Windows users would use the value 13 instead.
  94.             break;           //Exit loop
  95.         else if(ch==127)     //127 is the ASCII value for the "backspace" key. Windows users would use the value 8 instead.
  96.         {       if(i!=0)     //This is for avoiding the input from being deleted
  97.             {
  98.                 printf("\b");       //'\b' represents blackspace escape character
  99.                 printf(" ");
  100.                 printf("\b");       //'\b' represents blackspace escape character
  101.                 i--;                //Go back to the array position that had the character that was removed by backspace
  102.                 pwd[i]='\0';        //End of array
  103.             }
  104.             else
  105.                 continue;
  106.         }
  107.         else
  108.         {
  109.             putchar('*');         //Replaces each character type by a *
  110.             pwd[i]=ch;            //Adds the character typed to the pwd array
  111.             i++;                  //Move to the next postion in the array
  112.         }
  113.     }
  114.     puts("\n \t \t \t |-----------------------------|");
  115.     pwd[i]='\0';
  116.     if((strcmp(uid,s_uid))==0 && (strcmp(pwd,s_pwd))==0) //checks if the user input is the same as what was set in the code (Admin, p@$$20rd)
  117.     {
  118.         printf("\t \t \t |\t USER AUTHENTICATED    |");
  119.         puts("\n \t \t \t |-----------------------------|");
  120.         sleep(1); //Pause the program for 1 second. Windows users would have to change this function to Sleep(1000) to get the same result
  121.         system("clear"); //"clear" is for unix users. Windows users whould use "cls" instead of "clear"
  122.     }
  123.     else
  124.     {
  125.         printf("\t \a \t \t |UNABLE TO AUTHENTICATE USER, |\n \t \t \t | \t    Try again\t       |");
  126.         puts("\n \t \t \t |-----------------------------|");
  127.         sleep(2); //Pause the program for 2 second. Windows users would have to change this function to Sleep(2000) to get the same result
  128.         system("clear"); //"clear" is for unix users. Windows users whould use "cls" instead of "clear"
  129.         fflush(stdin);
  130.         login(); //Recall the login function (Recursion)
  131.     }
  132. }
  133. //-----------------------------------------------------------------------------------------
  134.  
  135.  
  136. //------------------- GETCH FUNCTION -----------------------------
  137. /**BECAUSE UNIX SYSTEMS DON'T SUPPORT THE CONIO.H LIBRARY THAT CONTAINS IT'S OWN BUILT IN GETCH() FUNCTION
  138. WE HAVE TO MANUALLY BUILD A GETCH FUNCTION SUPPORTED BY THE TERMIOS.H LIBRARY. WINDOWS USERS CAN USE INCLUDE
  139. THE CONIO.H AND REMOVE THIS FUNCTION**/
  140.  
  141. int getch(void)
  142. {
  143.     struct termios oldattr, newattr;
  144.     int ch;
  145.     tcgetattr( STDIN_FILENO, &oldattr );
  146.     newattr = oldattr;
  147.     newattr.c_lflag &= ~( ICANON | ECHO);//knock down keybuffer
  148.     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
  149.     system("stty -echo");//shell out to kill echo
  150.     ch = getchar();
  151.     system("stty echo");
  152.     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
  153.     return ch;
  154. }
  155. //-----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement