Advertisement
Guest User

Untitled

a guest
Mar 30th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. // Precompiled header file
  2. // It's used by Microsoft Visual Studio to let the compiler know the files that are once compiled and no need to compile it from scratch
  3. // idk exactly... Visual Studio requires it for some stupid ass reason
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. // This is used in the askPassword function
  8. // allows me to use _getch and _putch functions
  9. #include <conio.h>
  10.  
  11. // So I don't have to do std:: everywhere
  12. using namespace std;
  13.  
  14. // This is our "records"
  15. string username = "admin";
  16. string password = "password";
  17.  
  18. // Define the methods before we use them so the compiler knows they exist
  19. bool askUsername();
  20. bool askPassword();
  21.  
  22. int main()
  23. {
  24.     bool isLoggedIn = false;
  25.    
  26.     // Welcome message
  27.     cout << "--- Welcome ---" << endl;
  28.    
  29.     // If we enter the wrong details, this will keep looping
  30.     while (!isLoggedIn)
  31.     {
  32.         while (!askUsername())
  33.         {
  34.             // They put in a username we don't have in our "records"
  35.             cout << "Unrecognized user" << endl;
  36.         }
  37.  
  38.         if (!askPassword())
  39.         {
  40.             // Password doesn't match our "records"
  41.             cout << "Incorrect Username/Password" << endl;
  42.         }
  43.  
  44.         // Set this so we don't continue looping into oblivion
  45.         // We could just do break; but meh...
  46.         isLoggedIn = true;
  47.     }
  48.  
  49.     // Welcome the user
  50.     cout << "\\o Welcome " << username << " o/" << endl;
  51.     cout << "Press any key to logout...";
  52.  
  53.     cin.ignore();
  54.  
  55.     return 0;
  56. }
  57.  
  58. bool askUsername()
  59. {
  60.     // Temporary variable for storing user input
  61.     string iUsername;
  62.  
  63.     cout << "Username: ";
  64.     // This function is from <string> and allows us to get the input from cin
  65.     // and put it directly into our temporary variable
  66.     getline(cin, iUsername);
  67.  
  68.     // Return true only if the username is in our records
  69.     return (iUsername == username);
  70. }
  71.  
  72. bool askPassword()
  73. {
  74.     // Temp variables
  75.     string iPassword;
  76.     char c;
  77.  
  78.     cout << "Password: ";
  79.  
  80.     // _getch will basically get the character that the user inputs
  81.     while (c = _getch())
  82.     {
  83.         // If they input a new line, then they pressed enter and they're done inputting their password
  84.         if (c == '\n' || c == '\r') break;
  85.  
  86.         // Append the character to our temp variable
  87.         iPassword.push_back(c);
  88.         // Mask the input
  89.         _putch('*');
  90.     }
  91.  
  92.     cout << endl;
  93.  
  94.     // Return true only if the password is in our records
  95.     return (iPassword == password);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement