Help needed in do while, 2 variable code

Hey, i was wondering if i could get some help with this code, im not sure if i made a mistake or my design is just messed up. Its a program that ask's for a username and password. If access is granted both the values; username and password, need to be entered correctly. The problem is that if i enter a correct username but an incorrect password or vice-versa it shows access granted. I want to make it show the "access granted" text by entering both values correct only. Heres the code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string username;
string password;

do
{
if (username == "" && password == "")
{
cout << "Enter your username: \n";
cin >> username;
cout << "Enter your password: \n";
cin >> password;
}
else
{
cout << "Incorrect Password and/or Username!\n" << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
}} while (username != "administrator" && password != "bool"); // fix this - both wont check before granting access.
cout << "Access granted, Welcome administrator";
cin.ignore();
cin.get();
}
Thanks Heaps,
MasterSplint3r
Last edited on
@MasterSplint3r

Easy fix. Change your && (AND) in the while statement to || (OR). Otherwise, as long as one of the variables is equal to the check, it passes.

while (username != "admin" || password != "bool");
Thanks whitenite1 , works like a charm.
@whitenite1

Could you tell me how i would loop this program to limit the username and password set coming up only three times? An example would be say; to stop password crackers or something to limit their logins to only three attempts.

Thanks
@MasterSplint3r

This will do it.

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
// Passwords.cpp : main project file.
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string username = "";
	string password = "";
	int attempts = 0;

	do
	{
		if (username != "administrator" || password != "bool")
		{
			cout << "Enter your username: \n";
			cin >> username;
			cout << "Enter your password: \n";
			cin >> password;
		}
		if(username == "administrator" && password == "bool")
		{
			cout << "Access granted, Welcome Faizan" << endl << endl;
		}
		else
		{
			cout << "Incorrect Password and/or Username!" << endl;
			attempts++;
		}
	} while ((username != "administrator" || password != "bool") && attempts <3);
		if ( attempts == 3)
			cout << "Too many unsuccessful attempts, made. Program closing" << endl << endl;
		else // Run program here
		{
			cout << "\t\tM E N U" << endl << endl;
			cout << "< 1 > ----- Change username" << endl;
			cout << "< 2 > ----- Change password" << endl;
			cout << "< 3 > ----- Quit program" << endl;
		}
		
		cin.ignore();
		cin.get();
}


EDIT:: Had the two username check spots with different texts. Corrected.

And also, glad I could be of help, MasterSplint3r
Last edited on
Thank you :)
Topic archived. No new replies allowed.