Advertisement
Guest User

Untitled

a guest
May 21st, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5.  
  6.  
  7. struct date
  8. {
  9.     char usr_id[30]; //username confirmation
  10.     int dd;
  11.     int mm;
  12.     int yy;
  13. }dob;
  14. struct u_address
  15. {
  16.     char usr_id[30]; //username confirmation
  17.     char nation[10];
  18.     char city[10];
  19.     char tem_add[10];
  20.     char perm_add[10];
  21.     char ward_zip[10];
  22. }a;
  23.  
  24. struct confidential_info
  25. {
  26.     char username[30];
  27.     char password[30];
  28. }usr_pswd;
  29.  
  30. struct general_info
  31. {
  32.     char f_name[30];
  33.     char l_name[30];
  34.     char ph_no[20];
  35.     int age;
  36.     char email[40];
  37.     int pin;
  38.     char password[25];
  39.     char username[30];
  40.  
  41.     double user_balance;
  42.  
  43. }g_info;
  44. /*
  45. Functions protoypes are defined here:
  46. they are 1)account creation
  47.          2)registration form
  48.          3)pin and username generator (random)
  49.          4)account viewing (reading account information)
  50. */
  51.  
  52.  
  53. /*
  54.     To generate security login/pass for users, a random 4 digit pin is generated for users using rand() and srand
  55.     ,and an account id is provided by the program. The id will have the following structure:
  56.     *User's name*+*random string of length 4*
  57.     and the password will use random generator for number of length 4, which will be stored in a dat or binary
  58.     file.
  59. */
  60. //Misc function
  61. void starline();
  62. void user_menu(char[]);
  63.  
  64. //Account creation
  65. void account_create();
  66. int pin_user();
  67. void account_info();
  68. int unique_user(char[]);
  69. void read_account_info(char[]);
  70.  
  71.  
  72. //for logging in
  73. int loginscreen();
  74. int login(char [],char []);
  75.  
  76. //User activity functions
  77. void checkbalance(char[]);
  78. void checkbookrequest(char[]);
  79. void bankoperatingtime(char[]);
  80. void transferfund(char[]);
  81. void branch_ATM_locations(char[]);
  82. void loan_status(char[]);
  83. void change_password_pin(char[]);
  84.  
  85. //defintions
  86. /*user menu for main function, this function needs to called repetedly, after user completeltes a certain
  87. task, it also should have logoff option if possible
  88. */
  89.  
  90. //balance check
  91.  
  92. void checkbalance(char username[])
  93. {
  94.     FILE *fp;
  95.     fp=fopen("User_Database_01.dat","r");
  96.     while(!feof(fp))
  97.     {
  98.         fread(&g_info,sizeof(g_info),1,fp);
  99.         if(strcmp(username,g_info.username)==0)
  100.         {
  101.             printf("\n\tCurrent Balance is %lf.",g_info.user_balance);
  102.             fclose(fp);
  103.             break;
  104.         }
  105.         else
  106.             printf("Couldn't find balance, ERROR!");
  107.             fclose(fp);
  108.             break;
  109.     }
  110.     fclose(fp);
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. //Menu function
  122.  void user_menu(char username[])
  123.  {
  124.     FILE *fp;
  125.     fp=fopen("User_Database_01.dat","r");
  126.     int choice;
  127.     char repeat;
  128.     printf("\n\n");
  129.     starline();
  130.  
  131.     printf("\n\nWelcome To User Menu");
  132.     printf("\n\t1.Check Balance");
  133.     printf("\n\t2.Check Book Request");
  134.     printf("\n\t3.Check Bank's Operating Hours");
  135.     printf("\n\t4.Inquire Branch & ATM locations");
  136.     printf("\n\t5.Check Loan Status");
  137.     printf("\n\t6.Change Password or PIN");
  138.     printf("\n\t7.View Account Info");
  139.     printf("\n\n");
  140.     printf("\n\tPlease choose an option:");
  141.     scanf("%d",&choice);
  142.     do
  143.     {
  144.     switch(choice)
  145.     {
  146.         case 1:
  147.         printf("\n\tPlease Wait");
  148.         checkbalance(username);
  149.         break;
  150.  
  151.         case 2:
  152.         printf("\n\tPlease Wait");
  153.  
  154.         break;
  155.  
  156.         case 3:
  157.         printf("\n\tPlease Wait");
  158.  
  159.         break;
  160.  
  161.         case 4:
  162.         printf("\n\tPlease Wait");
  163.  
  164.         break;
  165.  
  166.         case 5:
  167.         printf("\n\tPlease Wait");
  168.  
  169.         break;
  170.  
  171.         case 6:
  172.         printf("\n\tPlease Wait");
  173.  
  174.         break;
  175.  
  176.         case 7:
  177.             printf("\n\nPlease Wait:");
  178.            read_account_info(username);
  179.  
  180.         default:
  181.             printf("\nPlease choose again.");
  182.         exit(1);
  183.     }
  184.     printf("\nWould you like to access the menu again? (Y/N)");
  185.     scanf("%c",&repeat);
  186.     }
  187.     while(repeat=='y' || repeat=='Y');
  188. }
  189.  
  190.  
  191.  
  192.  
  193. int unique_user(char incoming[])
  194. {
  195.     FILE *fp;
  196.     fp=fopen("User_Database_01.dat","r");
  197.     while(!feof(fp))
  198.     {
  199.         if(incoming==g_info.username)
  200.  
  201.             {
  202.             fclose(fp);
  203.             return 1;
  204.             break;
  205.             }
  206.         else
  207.             {
  208.             return 0;
  209.             break;
  210.             }
  211.  
  212.     }
  213. }
  214.  
  215. void starline() //a multi-purpose seperator
  216. {
  217.     int i;
  218.     for(i=0;i<40;i++)
  219.     {
  220.         printf("***");
  221.     }
  222. }
  223.  
  224. //Function to create pin number, and the suffix for username
  225.  
  226. int pin_user()
  227. {
  228.     {
  229.     srand(time(NULL));
  230.     //This is a seed for random generator, using the current time.
  231.     int random_pin=0;
  232.     int i,j=0;
  233.     while(j<4)
  234.     {
  235.         i=rand()%9;
  236.         random_pin=random_pin*10+i;
  237.         //This creates a 4-digit pin at the end of the loop
  238.         j++;
  239.  
  240.     }
  241.     return random_pin;
  242.     //return value is used in the registration function to get the pin to the user.
  243.  
  244.     }
  245.  
  246. }
  247.  
  248. //Function to create account
  249. void account_create()
  250. {
  251.     FILE *fp;
  252.     FILE *fp1,*fp2,*fp3;
  253.     int suffix_int,i;
  254.     char username[34];  //suffix for a username
  255.     int pin;    //pin for 2 step authentication
  256.     printf("\n\t\t\t\t\tWelcome to account creation");
  257.     printf("\n\n");
  258.     starline();
  259.     fp=fopen("User_Database_01.dat","a");
  260.     fflush(stdin);
  261.     printf("\nEnter first name:");
  262.     gets(g_info.f_name);
  263.     fflush(stdin);
  264.     printf("\nEnter your last name:");
  265.     gets(g_info.l_name);
  266.     fflush(stdin);
  267.     printf("\nEnter contact number:");
  268.     gets(g_info.ph_no);
  269.     printf("\nEnter your age:");
  270.     scanf("%d",&g_info.age);
  271.     fflush(stdin);
  272.     printf("\nEnter your email address:");
  273.     gets(g_info.email);
  274.     g_info.user_balance=0.00;
  275.  
  276.     /*Now the program generates a random number
  277.     to assign the username, and create an account number,and create a 4
  278.     digit pin, which is used to log in along with the password.*/
  279.  
  280.     printf("\tPlease wait while we generate a pin number.");
  281.     pin=pin_user();
  282.     printf("\a");
  283.     printf("\nYour pin number is %04d",pin);
  284.     printf("\nPlease keep your pin secure, and do not tell it to anyone.");
  285.     g_info.pin=pin;
  286.     fflush(stdin);
  287.     printf("\nPlease create a secure password:");
  288.     gets(g_info.password);
  289.     RETRYING:g_info.pin=pin;
  290.     //creating pin for authentication, with a GOTO marker for retrying.
  291.  
  292.     //now we create a suffix to generate a username
  293.     suffix_int=pin_user();
  294.     sprintf(username,"%s%04d",g_info.f_name,suffix_int);
  295.     //sprintf combines integer and the string to create a username.
  296.  
  297.     i=unique_user(username);
  298.     if(i==0)
  299.     {
  300.         printf("\nYour username is:%s",username);
  301.         USR_CONFIRM:printf("\nPlease re-type your username for confirmation:");
  302.         //puts(g_info.username);
  303.     }
  304.  
  305.     else goto RETRYING;
  306.     fflush(stdin);
  307.     //re-confirmation stores the username in the struct's "username" now
  308.     gets(g_info.username);
  309.     fwrite(&g_info,sizeof(g_info),1,fp);
  310.     //Date of Birth input
  311.         printf("\nEnter date of birth");
  312.         //The format is yy/mm/dd.
  313.         //The output format would be the same.
  314.  
  315.     fp1=fopen("User_DOB.dat","a");
  316.     //opening file for DOB storage
  317.     printf("\n\tYear:");
  318.     scanf("%d",&dob.yy);
  319.     printf("\n\tMonth:");
  320.     scanf("%d",&dob.mm);
  321.     printf("\n\tDay:");
  322.     scanf("%d",&dob.dd);
  323.  
  324.     //opening file for User Addresses
  325.     fp3=fopen("User_addresses.dat","a");
  326.  
  327.     //Input for both temp and permanent address of user
  328.     printf("\nEnter Address");
  329.     fflush(stdin);
  330.     printf("\n\tCountry:");
  331.     gets(a.nation);  //"a" is the structure for address. Define inside g_info (general info)
  332.     fflush(stdin);
  333.     printf("\n\tCity:");
  334.     gets(a.city);
  335.     fflush(stdin);
  336.     printf("\n\tEnter temporary address:");
  337.     gets(a.tem_add);
  338.     fflush(stdin);
  339.     printf("\n\tEnter permanent address:");
  340.     gets(a.perm_add);
  341.     fflush(stdin);
  342.     printf("\n\tEnter Ward/Zip code:");
  343.     gets(a.ward_zip);
  344.     printf("\a");
  345.  
  346.  
  347.     printf("\a");
  348.     //opening file for Confidential info
  349.     fp2=fopen("Confidental.dat","a");
  350.     //copying password and username to a seperate file for quick access.
  351.  
  352. //    This string will be used to confirm and display information for all the files.
  353. //   For this purpose, we are now copying it throughout all the three files.
  354.  
  355.  
  356.     strcpy(usr_pswd.username,g_info.username);
  357.     strcpy(usr_pswd.password,g_info.password);
  358.     fwrite(&usr_pswd,sizeof(usr_pswd),1,fp2);
  359.     //copying usernames to other files for verification
  360.  
  361.     strcpy(dob.usr_id,g_info.username);
  362.     strcpy(a.usr_id,g_info.username);
  363.     fwrite(&a,sizeof(a),1,fp3);
  364.     fwrite(&dob,sizeof(dob),1,fp1);
  365.  
  366.     fclose(fp);
  367.     fclose(fp2);
  368.     fclose(fp1);
  369.     fclose(fp3);
  370. }
  371.  
  372. //The login screen function
  373. int loginscreen()
  374. {
  375.     FILE *fp;
  376.     int confirm;
  377.     char user[40],password[40];
  378.     int compare;
  379.     fp=fopen("User_Database_01.dat","r");
  380.     ATTEMPT:printf("\nEnter username:");
  381.     scanf("%s",user);
  382.     printf("\nEnter password:");
  383.     scanf("%s",password);
  384.  
  385.     confirm=login(user,password);
  386.  
  387.     if(confirm==1)
  388.         {
  389.  
  390.             user_menu(user); //calling the menu from login-scree, with username in it.
  391.         }
  392.     else
  393.         printf("\nPlease try again, the credentials are incorrect.");
  394.         goto ATTEMPT;
  395. }
  396.  
  397. //Reading or printing account info
  398. void read_account_info(char user[])
  399. {
  400.     printf("\n\n");
  401.     int compare;
  402.     starline();
  403.     printf("\nShowing user info for:");
  404.     puts(user);
  405.     FILE *fp1;
  406.     fp1=fopen("User_Database_01.dat","r");
  407.     while(!feof(fp1))
  408.     {
  409.         fread(&g_info,sizeof(g_info),1,fp1);
  410.         if(strcmp(g_info.username,user))
  411.         {
  412.             printf("\nFirst Name:%s",g_info.f_name);
  413.             printf("\nLast Name:%s",g_info.l_name);
  414.             printf("\nContact Number:%s",g_info.ph_no);
  415.             printf("\nAge:%d",g_info.age);
  416.             printf("\nPIN number:%d",g_info.pin);
  417.             printf("\nUsername:%s",g_info.username);
  418.             printf("\nPassword:%s",g_info.password);
  419.             printf("\nEmail:%s",g_info.email);
  420.  
  421.         }
  422.  
  423.     }
  424. }
  425.  
  426.  
  427.  
  428. //Login user and password check
  429. int login(char u[],char p[])
  430. {
  431.     FILE *fp;
  432.     fp=fopen("Confidental.dat","r");
  433.  
  434.     while(!feof(fp))
  435.     {
  436.         fread(&usr_pswd,sizeof(usr_pswd),1,fp);
  437.         if(!strcmp(u,usr_pswd.username) && !strcmp(p,usr_pswd.password))
  438.         {
  439.             fclose(fp);
  440.             return 1;
  441.         }
  442.  
  443.     }
  444.     return 0;
  445. }
  446.  
  447. //main function
  448. int main()
  449. {
  450.  
  451.  
  452.  
  453.  
  454.     loginscreen();
  455.     /* if(confirm==0)
  456.     {
  457.         printf("\a\n\tAccess Approved.");
  458.         //switch case for menu:
  459.         printf("\n\n");
  460.     }
  461.     else
  462.     {
  463.         //Acces denied
  464.         printf("\a\n\tAccess Denied.");
  465.     }
  466.     */
  467.     printf("\n\n");
  468.     starline();
  469.     return 0;
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement