While loop if statements problem

How do I solve this? I can't get out of the loop. Every time I change my code around, something else screws up..

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
  #include <iostream>
#include <string>

using namespace std;

int main()
{

	string const userId = "someone";
	string const password = "something";
	string const backDoor = "someoneelse";
	string userId2;
	string password2;
	int loginTries = 0;

	cout << "Hello! Please login. " << endl; // login screen is presented


	

	while (loginTries < 3 || userId2 != backDoor)
	{
		cout << "UserId: ";	cin >> userId2;

		if (userId2 == userId || userId2 != backDoor)
		{
			cout << "Password: "; cin >> password2;

			if (userId2 != userId || password2 != password)
			{
				loginTries = loginTries + 1;
				cout << "Incorrect username or password" << endl;

			}
		}

	}
	system("pause");
	return 0;
So I think you just need to change your "||" to "&&".
I'm thinking and typing here.

I think the purpose of your backDoor is an auto-escape from the loop.
If I'm correct, then you don't need to check userId2 != backDoor in your first if-statement. Remove that and leave the first part if your logic in place.

This means you are only allowed an opportunity to enter the password IF you have chosen a registered ID.

This means in the second if-statement, you don't need userId2 != userId because that was already checked in the first if-statement.

Now all you would have to do is place the loginTries counter in the first if-statement and adjust the error message.

**Note I also think you're trying to not allow the person inputting the info to know which of the two inputs they (did) was wrong. So for that, I'll post a follow-up code that meets that criteria, since that's different than what I said above ^^^
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
#include <iostream>
#include <string>

using namespace std;

int main()
{

	string const userID = "ID";
	string const password = "password";
	string const backdoor = "ESCAPE";
	
        string userID2;
	string password2;
	int loginTries = 0;

	cout << "Hello! Please login. " << endl; // login screen is presented

	while (loginTries < 3 && userID2 != backdoor)
	{
		cout << "UserID: ";	cin >> userID2; 
                  cout << endl;

		if (userIdD != backdoor)
		{
			cout << "Password: "; cin >> password2;

			if (userID2 != userID || password2 != password)
			{
				loginTries++;
				cout << "Incorrect username or password" << endl;
			}
		}
	}

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.