Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Precompiled header file
- // 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
- // idk exactly... Visual Studio requires it for some stupid ass reason
- #include "pch.h"
- #include <iostream>
- #include <string>
- // This is used in the askPassword function
- // allows me to use _getch and _putch functions
- #include <conio.h>
- // So I don't have to do std:: everywhere
- using namespace std;
- // This is our "records"
- string username = "admin";
- string password = "password";
- // Define the methods before we use them so the compiler knows they exist
- bool askUsername();
- bool askPassword();
- int main()
- {
- bool isLoggedIn = false;
- // Welcome message
- cout << "--- Welcome ---" << endl;
- // If we enter the wrong details, this will keep looping
- while (!isLoggedIn)
- {
- while (!askUsername())
- {
- // They put in a username we don't have in our "records"
- cout << "Unrecognized user" << endl;
- }
- if (!askPassword())
- {
- // Password doesn't match our "records"
- cout << "Incorrect Username/Password" << endl;
- }
- // Set this so we don't continue looping into oblivion
- // We could just do break; but meh...
- isLoggedIn = true;
- }
- // Welcome the user
- cout << "\\o Welcome " << username << " o/" << endl;
- cout << "Press any key to logout...";
- cin.ignore();
- return 0;
- }
- bool askUsername()
- {
- // Temporary variable for storing user input
- string iUsername;
- cout << "Username: ";
- // This function is from <string> and allows us to get the input from cin
- // and put it directly into our temporary variable
- getline(cin, iUsername);
- // Return true only if the username is in our records
- return (iUsername == username);
- }
- bool askPassword()
- {
- // Temp variables
- string iPassword;
- char c;
- cout << "Password: ";
- // _getch will basically get the character that the user inputs
- while (c = _getch())
- {
- // If they input a new line, then they pressed enter and they're done inputting their password
- if (c == '\n' || c == '\r') break;
- // Append the character to our temp variable
- iPassword.push_back(c);
- // Mask the input
- _putch('*');
- }
- cout << endl;
- // Return true only if the password is in our records
- return (iPassword == password);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement