Chapter 4 Practice Problem 4 Multiple user and passwords

Hello, I am writing here because I am stomped by an exercise problem I am doing in one of my c++ books from school. The problem states to create multiple user names and passwords program and to make it prompt the user again if first attempt failed. Here is my code...
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string username_01, username_02;
    string password_01, password_02;
    cout << "Enter a username: " << "\n";
    getline( cin, username_01, '\n');
    cout << "Enter your password: " << "\n";
    getline( cin, password_01, '\n' );
    if ( username_01 == "root" && password_01 == "toor" || username_02 == "door"        && password_02 == "knob" ) /* Knowtest the || Or Boolean operator*/ 
    {
    cout << "Access allowed " << "\n";
    }
    else
    {
        cout << "Bad password or username. Denied Access! " << "\n";
        //returning is a convinient way to stop the
        // program
        return 0;
    }
}


Tried it once, worked. Tried it this way... Failed. What happens is that the program runs, and accepts the first user if entered with password, but the second user is disregarded. Notice the || for an Or Boolean operator and the commas used when declaring the usernames and passwords as strings. I figured this is quicker but maybe the order, or maybe the shortness of the code is what makes it fail. Also I am not sure about the prompting again if user fails to enter once. i am pretty sure you have to use a Loop like while or For so that this is achieved. I am not sure, but thats in the next chapter. So please if anyone has some suggestions they would be greatly appreciated. Thanx
Last edited on
I'd guess that different usernames means that the user can do different things.
So you don't need username_02/password_02.
Just check username_01/password_01 against both (replace 2 with 1 on line 14)
I think you are only requesting 1 username and 1 password but check both of them...

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

using namespace std;

int main()
{
    string username;
    string password;
    cout << "Enter a username: " << "\n";
    cin >> username;
    cout << "Enter your password: " << "\n";
    cin >> password;
    if ( username == "root" && password == "toor" || username == "door" && password == "knob")
    {
         cout << "Access granted" << "\n";
    }
    else
    {
         cout << "Bad password or username. Access denied! " << "\n";
         //returning is a convinient way to stop the
         // program
         return 0;
    }
}
Thanx Mr Troll face and coder 777. You guys were great help. i successfully did the multiple passwords. You know though I even thought of adding a third string called user name and password, which would have confused me even more lol. This is because I know that overall the usernames and passwords were just "options" hence the Boolean ||. but thanx this really helped. Although I don't know about the prompting over if log in fails. I think its a loop thing and could only be done with with loops which is the next chapter.
Last edited on
So I've tried to make the user prompt me once more if first attempt fails by adding an If statement with a condition. Here is the code.
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string username;
    string password;

    cout << "Enter a username: " << "\n";
    cin >> username;
    cout << "Enter your password: " << "\n";
    cin >> password;


    if ( username == "root" && password == "toor" ||
        username == "door" && password == "knob")
    {
         cout << "Access granted" << "\n";
    }
    if ( username false && password false )
    {
    cout << "Enter a username: " << "\n";
    cin >> username;
    cout << "Enter your password: " << "\n";
    cin >> password;
    }

    else
    {
         cout << "Bad password or username. Access denied! " << "\n";
         //returning is a convinient way to stop the
         // program
         return 0;
    }
    
}
 

I personally think this should work, but I have only read the chapter twice. maybe I missed something or a lot. Any comments greatly appreciated...
Last edited on
Doing that is all well and good if you are only checking for 1 or 2 users, if you use arrays, there's a more convenient way to check for a lot of users (you will still need their usernames and passwords, but at least it won't be one massive if statement. Also, when dealing with compound conditions, it is often better to put parentheses around each set of conditions. This also allows you to control what functions a user has as you can assign a number to them and use that number to give the appropriate controls.

I'm not sure if you've gone over them in your book though.
Last edited on
So I've tried to make the user prompt me once more if first attempt fails by adding an If statement with a condition.

Yeah that won't work. you can just add a goto top; at the end of the code and a top: after
string username;
string password;


Here's the complete code:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string username;
    string password;
    int i = 0;
    top:
        i++;
        cout << "Enter a username: " << "\n";
        cin >> username;
        cout << "Enter your password: " << "\n";
        cin >> password;
        if ( username == "root" && password == "toor" || username == "door" && password == "knob")
        {
              cout << "Access granted" << "\n";
        }
        else
        {
             cout << "Bad password or username. Access denied! " << "\n";
             //returning is a convinient way to stop the
             // program
             return 0;
        }
    if (i < 10) /*Change 10 to the amount you want to prompt user*/
    {
         goto top;
    }
}


but at least it won't be one massive if statement.

You can also use else if (condition) instead of ||
You can also use else if (condition) instead of ||


Using else if there is worse, you have shorter conditions, but now you have a lot of redundant code.
closed account (3qX21hU5)
Actually don't use goto's they generally are very bad practice because they made the program hard to debug and read (IE You have no idea how the flow of the program works and need to spend a bunch of time scrolling up and down from goto to goto) among other things. So generally you will want to avoid them like the plague.

In this situation I would probably go with a while loop like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string username;
string password;

while(true)
{
    cout << "Enter a username: " << "\n";
    cin >> username;
    cout << "Enter your password: " << "\n";
    cin >> password;

    if ( username == "root" && password == "toor" || username == "door" && password == "knob")
    {
        cout << "Access granted" << "\n";
        break;    // Exits the loop
    }
    
    cout << "Bad password or username. Access denied! " << "\n";
}
Last edited on
@manudude03

There is a useful keyboard shortcut "CTRL + C" and then "CTRL + V"
@MrTrollFace

I do know the shortcuts for copy and paste, it still doesn't make it a good idea. Say you have 10 usernames/password combinations to check, are you going to put in 9 else if statements? What about 20 combinations? 100?
closed account (3qX21hU5)
I do know the shortcuts for copy and paste, it still doesn't make it a good idea. Say you have 10 usernames/password combinations to check, are you going to put in 9 else if statements? What about 20 combinations? 100?


manudude is correct you generally don't want to use if else in that situation. Specially when you get up to 100 or even 10 different combinations to check (Though then it would be best to use a container to cross reference with).
Last edited on
Ok, whatever, you can use case. What would you use manudude03?
No really I want to learn the write way
A quick program I threw together. Usually you'd be storing all the users and passwords in an external file and either having a number of records or use a vector, but for demo's sake...

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
44
45
46
47
48
#include <iostream>

using namespace std;

string username[10]={"user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9", "user10"};
string password[10]={"pass1", "pass2", "pass3", "pass4", "pass5", "pass6", "pass7", "pass8", "pass9", "pass10"};
string user="";
string pass="";

int LoginCheck (string username[], string password[], string user, string pass)
{
    for (int i=0; i<10; i++)
    {
        if (user==username[i] && pass==password[i])
            return i+1; // I like to use 0 as a false return
    }
    return 0;
}

int main()
{
    int loginattempts=0;
    while (LoginCheck(username, password, user, pass)==0)
    {
        loginattempts++;
        cout << "Username: ";
        cin >> user;
        cout << "Password: ";
        cin >> pass;
        if (LoginCheck(username, password, user, pass)!=0)
        {
            cout << "Welcome " << user << "." << endl;
            break; // not needed, braces aren't needed in this block either as only 1 line then
        }
        else if (loginattempts==3)
        {
            cout << "Maximum login attempts exceeded." << endl;
            break;
        }
        else
        {
            cout << "Invalid username/password combination" << endl;
        }
    }
    int a;
    cin >> a; // this has no purpose other than stopping the program closing automatically
    return 0;
}


Also, you don't need to set the initial user/pass as "", just declaring them is good enough.
Last edited on
That's wise. Good job
Topic archived. No new replies allowed.