Advertisement
jig487

LoginThing

Jun 29th, 2021 (edited)
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. bool isLoggedIn()
  8. {
  9.     string username, password, un, pw;
  10.  
  11.     cout << "Enter username: ";
  12.     cin >> username;
  13.     cout << "Enter Password: ";
  14.     cin >> password;
  15.  
  16.     ifstream read("c:\\" + username + ".txt");
  17.     getline(read, un);
  18.     getline(read, pw);
  19.  
  20.     if (un == username && pw == password)
  21.     {
  22.         return true;
  23.     }
  24.     else
  25.     {
  26.         return false;
  27.     }
  28. }
  29.  
  30. int main()
  31. {
  32.     int choice;
  33.  
  34.     cout << "1: Register\n2: Login\nYour choice: ";
  35.     cin >> choice;
  36.  
  37.     if (choice == 1)
  38.     {
  39.         string username, password;
  40.  
  41.         cout << "Select a username: ";
  42.         cin >> username;
  43.         cout << "Select a password: ";
  44.         cin >> password;
  45.  
  46.         ofstream file;
  47.         file.open("c:\\" + username + ".txt");
  48.         file << username << endl << password;
  49.         file.close();
  50.    
  51.         main();
  52.     }
  53.     else if (choice == 2)
  54.     {
  55.         bool status = isLoggedIn();
  56.  
  57.         if (!status)
  58.         {
  59.             cout<<"False login!"<<endl;
  60.             system("PAUSE");
  61.             return 0;
  62.         }
  63.         else
  64.         {
  65.             cout<<"Successfully logged in!"<<endl;
  66.             system("PAUSE");
  67.             return 1;
  68.  
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement