Created
March 13, 2021 22:24
-
-
Save Caalek/a047990036b0db6dcc878ffb989bf91a to your computer and use it in GitHub Desktop.
C++ login system in the console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int getChoice() { | |
cout << endl << "LOGIN SYSTEM" << endl; | |
cout << "(1) Create account" << endl; | |
cout << "(2) Login" << endl; | |
cout << "(3) Quit application" << endl; | |
int choice; | |
cin >> choice; | |
return choice; | |
} | |
string getEmail() { | |
string email; | |
cout << "Email: "; | |
cin >> email; | |
return email; | |
} | |
string getPassword() { | |
string password; | |
cout << "Password: "; | |
cin >> password; | |
return password; | |
} | |
bool auth(string email, string password) { | |
string line; | |
bool foundEmail; | |
fstream fs; | |
fs.open("data.txt", fstream::in); | |
while (getline(fs, line)) { | |
if (line == email) { | |
foundEmail = true; | |
} | |
if (line == password && foundEmail) { | |
fs.close(); | |
return true; | |
} | |
} | |
return false; | |
} | |
void createAccount(string email, string password) { | |
fstream fs; | |
fs.open("data.txt", fstream::app); | |
fs << endl << email << endl << password; | |
fs.close(); | |
} | |
int main() { | |
bool running = true; | |
string email, password; | |
int choice; | |
while (running) { | |
choice = getChoice(); | |
switch (choice) { | |
case 1: | |
email = getEmail(); | |
password = getPassword(); | |
createAccount(email, password); | |
cout << endl << "ACCOUNT CREATED" << endl; | |
break; | |
case 2: | |
email = getEmail(); | |
password = getPassword(); | |
if (auth(email, password) == true) { | |
cout << endl << "LOGGED IN" << endl; | |
break; | |
} | |
else { | |
cout << endl << "INVALID CREDENTIALS" << endl; | |
break; | |
} | |
case 3: | |
running = false; | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment