Guest User

Login function

a guest
Nov 19th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. User login()
  2. {
  3.     ifstream flogin("users.dat",ios::binary);
  4.     if(!flogin)
  5.     {
  6.          cout<<"No accounts have been created. Please restart and create an account first.";
  7.          getch();
  8.          exit(1);
  9.     }
  10.     else
  11.     {
  12.         char uname[15],pword[15];
  13.         User u;
  14.         int flag;
  15.         int counter=0;
  16.         a:
  17.         system("cls");
  18.         if(counter!=0)
  19.         {
  20.             flogin.open("users.dat",ios::binary);
  21.         }
  22.         flogin.seekg(0,ios::beg);
  23.         cout<<"Username: ";
  24.         fflush(stdin);
  25.         gets(uname);
  26.           while(flogin.read((char*)&u,sizeof(u)))
  27.         {
  28.             if(strcmp(uname,u.username)==0)
  29.             {
  30.                 b:
  31.                 flag=0;
  32.                 system("cls");
  33.                 cout<<"Password: ";
  34.                 fflush(stdin);
  35.                 gets(pword);
  36.                 if(strcmp(pword,u.password)==0)
  37.                 {
  38.                     break;
  39.                 }
  40.                 else
  41.                 {
  42.                     cout<<"Wrong password. Please try again.";
  43.                     getch();
  44.                     goto b;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 flag=1;
  50.             }
  51.         }
  52.  
  53.         if(flag==1)
  54.         {
  55.            cout<<"Username does not exist. Try again.("<<(3-counter)<<" tries left)";
  56.            getch();
  57.            flogin.close();
  58.            counter++;
  59.            if(counter>=4)
  60.            {
  61.                exit(1);
  62.            }
  63.            else
  64.            {
  65.               goto a;
  66.            }
  67.         }
  68.         else if(flag==0)
  69.         {
  70.            flogin.close();
  71.            getch();
  72.            return u;
  73.         }
  74.     }
  75. }
Add Comment
Please, Sign In to add comment