I have abandoned that last program and wrote this from scratch.
For anyone new to C++ that would like to use this, it is licensed under the GNU GPLv2 and can be found in Small-Projects-Cpp on Github. Just download “Userpass.zip” as it is not necessary to clone the entire repository.
#include <iostream>
#include <string>
#include <unistd.h>
#include <termios.h>
getpass() makes the password input show asterisks. Not exactly invisible, but it’s probably the best solution for that.
using namespace std;
string PASSWD;
string getPASS()
{
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
string PASS;
cout << "Password: "; cin >> PASS; cout << endl;
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return PASS;
}
MENU() has a switch/case menu but it is unrelated so i’m skipping it in the asnwer. You can replace it with whatever funtions you want.
int attempts = 0;
int main()
{
while (attempts == 3)
{
cout << "Too many attempts have been made! Exiting..." << endl; exit(0);
};
string USER;
cout << "Username: "; cin >> USER;
if (USER == "josh")
{
if (getPASS() == "hsoj")
{
cout << "\nAccess granted!\n" << endl;
MENU();
}
else
{
cout << "\nAccess denied!\n" << endl;
attempts = attempts + 1;
main();
};
}
else
{
cout << "\nAccess denied!\n" << endl;
attempts = attempts + 1;
main();
};
return 0;
}
solved username and password check