Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program only allows login if the email matches the last registered user. C++

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
12345@gmail.com
testing@gmail.com
fortified@gmail.com
Then it would only ever continue to the password section if I typed fortified@gmail.com in my program and it would not let me proceed if i typed any other emails.

>Solution :

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;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading