1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
#ifdef _WIN32
#include <conio.h>
#else
char getch(void);
char getche(void);
#endif
void pause();
void clear();
string getpass()
{
string pass;
for (char ch; ch != '\r' && ch != '\n'; )
{
ch = getch();
if (ch == '\b' || ch == '\x7f')
{
if (pass.size() > 0)
{
cout << "\b \b" << flush;
pass.pop_back();
}
}
else if (ch != '\r' && ch != '\n')
{
pass += ch;
cout << '*' << flush;
}
}
cout << endl;
return pass;
}
void signup(string& username, string& password)
{
for (bool accepted = false; !accepted; )
{
clear();
cout << "SIGN-UP FOR AN ACCOUNT\n";
cout << "Enter username: ";
getline(cin, username);
cout << "Enter password: ";
password = getpass();
bool hasup = false, haslow = false, hasalpha = false;
if (password.size() >= 6 && password.size() <= 10)
for (size_t i = 0; i < password.size(); i++)
{
hasup |= isupper(password[i]);
haslow |= islower(password[i]);
hasalpha |= isalpha(password[i]);
}
if (haslow && hasup && hasalpha)
{
cout << "Sign in successful!\n";
accepted = true;
}
else
cout << "Password should have at least 1 uppercase and"
"lowercase letter, 1 digit and 6 to 10 characters.\n";
pause();
}
}
void login(const string& username, const string& password)
{
string login_user, login_pass;
while (login_user != username || login_pass != password)
{
clear();
cout << "LOG IN TO YOUR ACCOUNT\n";
cout << "Enter username: ";
getline(cin, login_user);
cout << "Enter password: ";
login_pass = getpass();
if (login_user != username || login_pass != password)
{
cout << "Invalid username or password\n";
pause();
}
}
}
int main ()
{
clear();
cout << "You must first create an account\n";
pause();
string username, password;
signup(username, password);
login(username, password);
clear();
cout << "WELCOME TO CEA\n"
<< "You are now logged in!\n";
pause();
clear();
}
#ifdef _WIN32
#include <cstdlib>
void pause() { system("pause"); }
void clear() { system("cls"); }
#else
#include <termios.h>
#include <cstdio> // getchar
static struct termios old, current;
void initTermios(int echo) {
tcgetattr(0, &old);
current = old;
current.c_lflag &= ~ICANON;
if (echo) current.c_lflag |= ECHO;
else current.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, ¤t);
}
void resetTermios(void) { tcsetattr(0, TCSANOW, &old); }
char getch_(int echo) {
initTermios(echo);
char ch = getchar();
resetTermios();
return ch;
}
char getch(void) { return getch_(0); }
char getche(void) { return getch_(1); }
void pause() {
std::cout << "Press any key to continue...";
getch();
}
void clear() { std::cout << "\x1b[H\x1b[J"; }
#endif
|