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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string username{"null"};
string password{"null"};
fstream userData;
void signin();
void signup();
int main()
{
userData.open("userdata.txt", ios::in);
if(userData.is_open()){
cout << "File opened successfully" << endl;
while(!userData.eof()){
getline(userData, username, '\n');
getline(userData, password, '\n');
}
if(username != "null" && password != "null")
signin();
else
signup();
}
else
cout << "Error opening file" << endl;
return 0;
}
void signin(){
string anw{"n"};
string tempUname{""};
string tempPword{""};
cout << "Account detected. Sign in? [Y/N]" << endl;
getline(cin, anw);
if(anw == "y"){
cout << "Enter username: " << endl;
getline(cin, tempUname);
cout << "Enter password: " << endl;
getline(cin, tempPword);
if(tempUname == username && tempPword == password)
cout << "Welcome!" << endl;
else{
cout << "Incorrect username or password. Please try again." << endl;
signin();
}
}
else if(anw == "n")
return;
else{
cout << "response not recognized: " << anw << endl;
signin();
}
}
void signup(){
string anw{"n"};
string validPword{""};
cout << "No account detected. Create one? [Y/N]" << endl;
getline(cin, anw);
if(anw == "y"){
cout << "Create username: ";
getline(cin, username);
cout << "Create password: ";
getline(cin, password);
cout << "Confirm password: ";
getline(cin, validPword);
if(password == validPword){
userData.open("userdata.txt", ios::out);
if(userData.is_open()){
cout << "Welcome! saving credentials...." << endl;
userData << username << endl;
userData << password << endl;
userData.close();
}
else{
cout << "error opening file" << endl;
}
}
else{
cout << "Passwords do not match." << endl;
signup();
}
}
else if(anw == "n")
return;
else{
cout << "response not recognized: " << anw << endl;
}
}
|