What am I doing wrong? While loop and if statements

This is for an assignment. I've been at it for hours and hours yet can't seem to get it right.

First ask for the userid. If the user id matches the special “back-door” user id, then no password should be requested or required.

If the userid does not match the backdoor, request a password.

Then using only a single if - else condition statement check if the user ID and password are correct or if the back-door ID was entered. This will require the use of the || and && logical operators.

If either don’t match, then a “Incorrect userid or password” message is displayed.

The user has up to 3 tries to log in. If they fail, then an “Access denied” message of your choosing is displayed, and the program exits.
__________

Can someone explain to me how to get out of the while loop?
Does the bold mean just an else and an else if?
And how would I go about making the access denied.

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

using namespace std;

int main()
{
	string userName, passWord;
	const string masterUser = "Mperson";
	const string masterPass = "masterP";
	const string specialUser = "vipPerson"; //stored as “string constants” so they can’t be changed.

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

	while (userName != specialUser)
	{
		cout << "What is your username? "; cin >> userName;
				
		if (userName == masterUser)
		{
				cout << "Enter password: "; cin >> passWord;
		}
		
		else if (passWord != masterPass && userName != specialUser && userName != masterUser)
		{
			cout << "Error. Invalid username or password." << endl;
		}
	}
	
	cout << "no password required" << endl;
	

	system("pause");
	return 0;
Last edited on
closed account (48T7M4Gy)
Perhaps you might show us what code you have written so far. :-)
The program asks the user to input a userid and a password which should be declared.

1
2
3
4
 string const userId = your master username;
string const password = your master password;
string const userId2;
string const password2;


then you ask the user for the userid:

1
2
 cout << "Please enter userid: ";
cin >> userId2;


if the userid that the user inputted matches the userId then no password needed:

1
2
3
4
5
6
7
 if ( userId == userId2 )
{
cout << "login successful";
}

do{ cout << "invalid userID, enter password" << endl;
cin >> password;} while (userId != userId2 );


Then using only one if - else statement, check if user ID and password are correct OR if user ID is entered.

1
2
3
4
 if ( userId != userId2 && password != password2 )
{
cout << "Incorrect userid or password";
}


you can put all of this inside a while loop that is gonna run 3 times.

while ( int login_tries = 0; login_tries < 3; login_tries++)
Last edited on
Thanks! I was looking at it the wrong way. You've helped alot.
Topic archived. No new replies allowed.