Guest User

Untitled

a guest
Dec 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #define FIELD_SIZE 25
  6. #define length(array) ( sizeof (array) / sizeof (array)[i] )
  7.  
  8. struct account
  9. {
  10.     char *id;
  11.     char *password;
  12. }
  13. static struct account accounts[] =
  14. {
  15.     {
  16.         "user", "pass"
  17.     }
  18.     ,
  19. }
  20. int is_authorized ( const char *uid, const char *pwd )
  21. {
  22.     int i;
  23.     for ( i = 0;
  24.     i <
  25.     length ( accounts );
  26.     i++ )
  27.     {
  28.         if ( stricmp ( uid, accounts[i].id ) == 0 &&   strcmp ( pwd, accounts[i].password ) ==0 )
  29.         {
  30.             return 1;
  31.         }
  32.     }
  33.     return 0;
  34. }
  35. void get_password ( char *pwd, int size )
  36. {
  37.     int i = 0;
  38.     int ch;
  39.     while ( i <
  40.     size - 1 && ( ch = getch() ) != '\r' )
  41.     {
  42.         if ( ch == '\b' )
  43.         {
  44.             if ( i != 0 )
  45.             {
  46.                 printf ( "\b%c\b", ' ' );
  47.                 --i;
  48.             }
  49.         }
  50.         else
  51.         {
  52.             putchar ( 'x' );
  53.             pwd[i++] = (char)ch;
  54.         }
  55.     }
  56.     pwd[i] = '\0';
  57. }
  58. int main ( void )
  59. {
  60.     char uid[FIELD_SIZE];
  61.     char pwd[FIELD_SIZE];
  62.     int tries = 0;
  63.     printf ("BeefOS Logon Screen \n");
  64.     while(tries<3){//While tries is less than 3.
  65.         if(tries==3){
  66.             printf("\n\n\t\t 3 Failed attempts. BYE I THINK \n\a");
  67.             return 0; //returning 0 exits the program.. i think? you can try something else to exit the program
  68.         }
  69.         printf ( "\n\n\t User ID: " ); //ask user for username
  70.         fflush ( stdout ); //wait for their input?
  71.         if ( fgets ( uid, sizeof uid, stdin ) != NULL )//assuming the input is not empty
  72.         {
  73.             char *newline = strchr ( uid, '\n' );
  74.             if ( newline != NULL )   *newline = '\0';
  75.             printf ( "\n\t Password: " );//ask for the password
  76.             fflush ( stdout );//wait for their input?
  77.             get_password ( pwd, sizeof pwd );//maybe do some magic too
  78.             if ( is_authorized ( uid, pwd ) )//if the user is authorized
  79.             {
  80.                 printf("\n\n\n\n\n\t\t Logging in. Welcome to BeefOS 0.1a");
  81.                 break;//break out of the loop, continuing our program
  82.             }
  83.             else //if our user is not authorized
  84.             {
  85.                 printf("\n\n\t\t Logging in? LolNope.\n\a");
  86.                 tries = tries + 1;//increment the tries counter
  87.                 //then do nothing so that the loop happens again and it asks the user for username again
  88.             }
  89.         }
  90.     }
  91.    
  92.     int integer1;
  93.     int integer2;
  94.     int sum;
  95.     printf("\n\n\n\n\n Enter a digit: \n");
  96.     scanf("%d", &integer1 );
  97.     printf("\n Enter a digit: \n");
  98.     scanf("%d", &integer2 );
  99.     sum = integer1 + integer2;
  100.     printf("The addition of these two numbers equals %d\n", sum);
  101.     getchar();
  102.     return 0;
  103. }
Add Comment
Please, Sign In to add comment