password user check

Hello,


I recently started to get into c++, and i have a (probably) very simple question. I am supposed to write a password program, so far so good. Then i wanted to make the program work for multiple users, in this case

martin
monika
joerg

What i want the program to do is give out the message "invalid user" when another username is entered, and proceed to the password part of the program when one of those usernames is entered.
However (at this stage) the program always couts "Invalid user" even when the entered username is one of those three. Where am I thinking wrong?
Thanks in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string username;
cout << "Please Enter your User Name: ";
cin >> username;



if  ( ( username != "martin") ||  (username != "monika") ||  (username != "joerg"))

               {
                    cout << "Invalid User, please try again" << endl;
                    continue;

                }
            

http://www.cplusplus.com/reference/string/string/compare/
see example

Also:
if ( ( username != "martin") || (username != "monika") || (username != "joerg"))
will always be true.
Last edited on
closed account (3qX21hU5)
compare for strings is getting probably to advanced for him and really it isn't needed for this.

As tath said it will always be true for you if condition.

Here is why. The || operator is the OR operator. Basically it is always true because of this. Lets say you enter the name "martin", when you enter the if condition this is what is happening.

( username != "martin") is FALSE

(username != "monika") is TRUE

(username != "joerg") is TRUE

Since you are using the || operator you condition is saying, "If any one of the condition's are true it returns true. So even though the first one is false the other two are true so the condition returns true. Wow that was confusing even to me lol.

Anyways the operator you are looking for is the && operator instead of the ||. The && operator is the AND operator.

if (username != "martin" && username != "monika" && username != "joerg")

The above code is saying this. If all of them statements in the condition are true, the condition is true. But if any one of the statements are false the condition is false.

Hope that helps and sorry if I confused you a bit ;p
Last edited on
Thank you kind sir. Meanwhile i tried a different approach that worked for me. I realize this is probably crude code, but keep in mind i started one week ago :)

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <iostream>
#include <string>

using namespace std;

int main()

{

//Variablen

string username;
string password;
int x = 0;



    while (1)
    {

        cout << "Welcome to Cyberdyne Systems!" <<endl;
        cout << "Please Enter your User Name: ";
        cin >> username;

        if (username == "martin" || username == "monika" || username == "joerg")

            {

            while (1)

            {


            cout << "Enter your password: ";
            cin >> password;

                if ( (username == "martin" && password == "hund") ||
                     (username == "monika" && password == "katze") ||
                     (username == "joerg" && password == "maus")
                    )
                       {
                        cout << "Login succesful";
                        return 0;
                       }
                else break;

            }
            }
        else
            {
            cout <<"Invalid user" << endl;
            x++;
            if (x > 2)
            {
                cout << "Login failed";
                return 0;
            }

            else continue;
            }

    }
}


Sadly, i dont understand the difference between what went wrong the first time, and why its working this time. Maybe you can explain why the first code that i posted is faulty, while this one works perfectly.

Thanks in advance

Fauch


edit: Zereos post is what i was looking for. I suspected my use of boolean operators being faulty. Thank you all.
Last edited on
Topic archived. No new replies allowed.