I recently created a simple C++ Login Program (check code below). However, I am not fully convinced unless I can solve the following issues I have in mind. I really need somebody's help with the following:
If I run the program for the first time, obviously I must not login successfully because there's no existing account, what can I do so that if I choose login and enter a username and password the program will output "Account does not Exist"
I also want the program to detect if I entered a wrong username or password to an existing account
How can I add forget password function?
Thank you so much.
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
using namespace std;
int choice;
bool cinfail;
int confirmation;
string username, password, password2;
void MainMenu();
void writetofile(string username){
ofstream writefile;
string file = username+".txt";
writefile.open(file.c_str());
writefile << password;
writefile.close();
MainMenu();
}
void login(){
system("cls");
cout<<"Username: "<<endl;
cin>>username;
cout<<"Password: "<<endl;
cin>>password;
if((username == username) && (password == password2)){
cout<<"SUCCESSFUL LOGIN!";
}
else{
cout<<"INVALID USERNAME OR PASSWORD!"<<endl;
}
}
void RegisterPassword(){
cout<<"Please enter the password: "<<endl;
cin>>password;
cout<<"Please reenter your password: "<<endl;
cin>>password2;
if(password == password2){
cin.clear();
cin.ignore(10000, '\n');
writetofile(username);
exit(1);
}
else{
cout<<"Sorry, invalid password. Try again."<<endl;
RegisterPassword();
}
system("cls");
}
void registerme(){
system("cls");
cout<<"REGISTER ACCOUNT"<<endl;
cout<<"Please enter your username: "<<endl;
getline(cin, username);
cout<<"\nUsername - \"" <<username<< "\"\nConfirm? \n\n[1] Yes\n[2] No"<<endl;
cin>>confirmation;
if(confirmation == 1){
RegisterPassword();
}
else{
cout<<"Sorry, invalid input. Try again"<<endl;
cin.clear();
cin.ignore(10000, '\n');
registerme();
}
}
void exit(){
exit(0);
}
void MainMenu(){
cout<<"SIMPLE LOGIN PROGRAM by RZAM\n[1] Login\n[2] Register\n[3] Exit"<<endl;
cin>>choice;
do{
cinfail = cin.fail();
cin.clear();
cin.getline(10000,'\n');
}while(cinfail == true);{
switch(choice){
case 1:
login();
break;
case 2:
registerme();
break;
case 3:
exit();
}
}
}
main(){
MainMenu();
}