Advertisement
Dinmrmr

рабочая регистрация и авторизация

Dec 1st, 2016
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <clocale>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. struct User
  10. {
  11.     char login[255],
  12.         password[255];
  13. };
  14.  
  15. void login(), registr(), menu(), statusCheck();
  16. void statusCheck()
  17. {
  18.  
  19. }
  20.  
  21. void login()
  22. {
  23.     User userIn, readIn;
  24.     cout << "Введите логин: ";
  25.     cin >> userIn.login;
  26.     cout << "Введите пароль: ";
  27.     cin >> userIn.password;
  28.     ifstream in("users.dat", ios_base::binary);
  29.     bool flag = false;
  30.     while (!in.eof())
  31.     {
  32.         in.read(reinterpret_cast<char *>(&readIn), sizeof(User));
  33.         if (strcmp(userIn.login, readIn.login) == 0 && strcmp(userIn.password, readIn.password) == 0)
  34.         {
  35.             flag = true;
  36.             break;
  37.         }
  38.     }
  39.     in.close();
  40.  
  41.     flag ? cout << "Добро пожаловать в систему, " << userIn.login << "!\n" : cout << "Ошибка входа! Попробуйте еще разок.\n";
  42. }
  43.  
  44. void menu()
  45. {
  46.     cout << "Добро пожаловать!";
  47.     cout << "Войти или зарегистрироваться? (y/n)";
  48.     char temp;
  49.     cin >> temp;
  50.     if (temp == 'y')
  51.     {
  52.         login();
  53.     }
  54.     else if (temp == 'n')
  55.     {
  56.         registr();
  57.     }
  58. }
  59.  
  60. void registr()
  61. {
  62.     User dataUser;
  63.     ofstream out("users.dat", ios_base::binary | ios_base::app);
  64.     cout << "Enter login: ";
  65.     cin >> dataUser.login;
  66.     cout << "Enter password: ";
  67.     cin >> dataUser.password;
  68.     out.write(reinterpret_cast<char *>(&dataUser), sizeof(User));
  69.     out.close();
  70.     cout << "Спасибо за регистрацию, " << dataUser.login << "!" << endl;
  71. }
  72.  
  73. int main()
  74. {
  75.     setlocale(LC_ALL, "russian");
  76.     menu();
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement