Creating an user login/password console and my code is missing something?

I don't know if it's the order or if I'm missing something in the logic. My best guest is that I messed up in creating the loop, but I don't know what it would be.

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
#include <iostream>
using namespace std;

int main ()
{
    string newName;
    string newPassword;
    string loginName;
    string loginPassword;
    int loginAttempt = 0;

    while (loginAttempt < 5)
    {
        cout << "Please enter new user name: ";
        cin >> newName;
        cout << "Please enter new user password: ";
        cin >> newPassword;
        cout << "Enter login name: ";
        cin >> loginName;
        cout << "Enter password: ";
        cin >> loginPassword;
        if (loginName == newName && loginPassword == newPassword) 
        {
            cout << "Welcome " << loginName;
            break;
        }
        
        else
        {
            cout << "Invalid login attempt. Please try again.\n" << '\n';
            loginAttempt++;
        }
    }
    if (loginAttempt == 5)
    {
            cout << "Too many login attempts! The program will now terminate.";
            return 0;
    }

    
}

It works for me.
Output:
1
2
3
4
5
Please enter new user name: Anna
Please enter new user password: 1234
Enter login name: Anna
Enter password: 1234
Welcome Anna


Did you maybe use spaces in your username or password?
Topic archived. No new replies allowed.