SHARE
TWEET

C++ login (functional) (console)

notjacob Jan 14th, 2020 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. std::map<std::string, std::string> usermap;
  5.  
  6. void addUser(std::string username, std::string password) {
  7.     usermap[username] = password;
  8. }
  9. bool registered(std::string username) {
  10.     if (usermap.find(username) != usermap.end()) {
  11.         return true;
  12.     }
  13.     return false;
  14. }
  15. string getPassword(std::string username) {
  16.     if (registered(username)) {
  17.         return usermap[username];
  18.     }
  19.     return NULL;
  20. }
  21. bool passwordIsCorrect(std::string username, std::string password) {
  22.     if (registered(username)) {
  23.         if (usermap[username] == password) {
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.     return false;
  29. }
  30.  
  31.  
  32. void program() {
  33.     std::string huser;
  34.     std::string hpass;
  35.     int hop;
  36.     std::cout << "\nWelcome!\n1. login\n2. register\n";
  37.     std::cin >> hop;
  38.     if (hop == 1) {
  39.         std::cout << "Enter your username: \n";
  40.             std::cin >> huser;
  41.             if(registered(huser)) {
  42.                 std::cout << "Enter your password: \n";
  43.                 std::cin >> hpass;
  44.                 if (passwordIsCorrect(huser, hpass)) {
  45.                     std::cout << "Logged in!\n";
  46.                     program();
  47.                 } else {
  48.                     std::cout << "Invalid password!\n";
  49.                     program();
  50.                 }
  51.             } else {
  52.                 std::cout << "Invalid username!\n";
  53.                 program();
  54.             }
  55.         } else if (hop == 2) {
  56.             std::cout << "Enter a username: \n";
  57.             std::cin >> huser;
  58.             if (registered(huser)) {
  59.                 std::cout << "Already registered!\n";
  60.                 program();
  61.             } else {
  62.                 std::cout << "Enter a password: \n";
  63.                 std::cin >> hpass;
  64.                 addUser(huser, hpass);
  65.                 std::cout << "registered!\n";
  66.                 program();
  67.             }
  68.         }
  69. }
  70.  
  71. int main(int argc, char **argv) {
  72.    program();
  73.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top