C++ ifstream is reading last line only

I am working on a program that allows a user to register an account. When a user registers for an account, the username and password are output to a text file “database.txt”.

There is also an option for a user to search for their password by inputing their username if they forget their password. I find that this works fine when there is only one user registered in the .txt file database. However, when there is more than one user, the forgot password function returns the password of the most recent registered user, no matter which username is searched.

database.txt looks like this:

user1 111
user2 222
user3 333

No matter which user is entered to find the password, the function will always return 333, which is the most recent registered user’s password. How do I fix this so that whichever user is searched, their password will output?

Here is the function:

void forgot()
{   
int ch;
system("cls");
cout << "Forgotten? We're here to help." << endl;
cout << "Choose one of the options below: " << endl;
cout << "1. Forgot my password" << endl;
cout << "2. Forgot my username" << endl;
cout << endl;
cout << "Enter your choice: ";
cin >> ch;

switch(ch)
{
    case 1: // search for username to find password
    {
        int count = 0;
        string searchUser, su, sp;
        cout << "Enter your username: "; 
        cin >> searchUser;

        ifstream searchUserName("database.txt"); 
        while(searchUserName >> su >> sp)
        {
            if(su == searchUser)
            {
                count = 1;
            }
        }
        searchUserName.close();
        if(count == 1)
        {
            cout << "Account found!" << endl;
            cout << "Your password is: " << sp;
            cin.get();
            cin.get();
            system("cls");
            menu();
        }
        else 
        {
            cout << "Sorry, that user ID does not exist." << endl;
            cout << "Please contact our service team for more details." << endl;
            cin.get();
            cin.get();
            system("cls");
            menu();
        }
        break;
    }
    case 2: // search for password to find username
    {
        int count = 0;
        string searchPass, su2, sp2;
        cout << "Enter your password: "; 
        cin >> searchPass;

        ifstream searchPassword("database.txt");
        while(searchPassword >> su2 >> sp2)
        {
            if(sp2 == searchPass)
            {
                count = 1;
            }
        }
        searchPassword.close();
        if(count == 1)
        {
            cout << "Your password has been found." << endl;
            cout << "Your username is: " << su2;
            cin.get();
            cin.get();
            system("cls");
            menu();
        }
        else
        {
            cout << "Sorry, we couldn't find your password in our database." << endl;
            cout << "Please contact our team for more information." << endl; 
            cin.get();
            cin.get();
            system("cls");
            menu();
        }

        break;
    }
    default: 
        cout << "Invalid choice, please try again." << endl;
        system("cls");
        forgot();
}

}

Answer

You should break out of the while loop once you have found the user name here:

        while(searchUserName >> su >> sp)
        {
            if(su == searchUser)
            {
                count = 1;
                break; // add this
            }
        }

Now it will continue to overwrite a previously found user name until searchUserName >> su >> sp is no longer true which is once it encounters EOF in most cases.

The other part of the program has the same problem.

Personnally I would rewrite the code such that the condition whether a matching password has been found or not is part of the loop condition.