So, I've been trying to make this program this entire day and I got stuck at this big problem where I cannot it to log into the account by verifying with the previously existing data in my txt file of registered users.
NOTE: userEmail
is inheritted from another class and is defined.
This is my login function:
void logIn() { // ! Function to log into an existing account.
system("cls");
string loginEmail, loginPassword;
bool verifyEmail = false;
cout<<"\n~\t~\tLogging into an existing account..\t~\t~\n";
cout<<"\nEmail: ";
cin.ignore();
getline(cin,loginEmail);
ifstream checkEmail("registeredUsers.txt");
ifstream open("registeredUsers.txt");
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
}
else {
verifyEmail = false;
}
}
if (verifyEmail == true) {
cout<<"\nPassword: ";
getline(cin,loginPassword);
}
else {
cout<<"Email not found.. Please register a new account.\n";
system("pause");
return;
}
system("pause");
}
Lets say if my registeredAccounts.txt has lines
[email protected]
[email protected]
[email protected]
Then it would only ever continue to the password section if I typed [email protected]
in my program and it would not let me proceed if i typed any other emails.
CodePudding user response:
Break out of the while loop immediately after you set verifyEmail = true. There is no need to check additional email addresses if you have already found a match. As it is, your code goes on to check non-matching email addresses, setting verifyEmail back to false.
For example:
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
break;
} else {
verifyEmail = false;
}
}
Now, you don't actually need to set verifyEmail to false. So you can just use:
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
break;
}
}