&& and || in a while loop

Hey everyone. First post here. I am new to programming and C++ so please be gentle! I am approaching with respect of your expertise and I truly appreciate any responses you can give to help me down the path.

Currently I am reading Jumping into C++ by Alex Allain, and I am trying to work through some problems. Here is the code I have come up with for a username and password checker. I included my question in the comments. THANK YOU ahead of time for any direction you can provide. I am here to learn!!

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
66
67
68
69
// I am brand new to programming and C++.  I am working my way
// through Jumping into C++ and I am trying to make sense of Chapters 
// 3-5.  In developing the username / password problem at the end of 
// chapter 4, I have run into this question and it has me stumped.
// I am using Code Blocks for the IDE.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string username;
    string password;
    string choice;

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

    while (password != "pass" || username != "tom")

// The program behaves correctly only if I use the OR operator.
// Why does the OR operator act like this in this case?
// I thought the correct solution would be the AND operator?

// While password is "fitz" and username is "tom", the expression is
// ( true || false ) which should evaluate to true, correct?
// ... which should then exit the loop and display the "You got it
// right" message.  Right?  But it doesn't.

// If I use the AND operator like this:
// while (password != "pass" && username != "tom")
// the program jumps out of the loop incorrectly.
// If username is "tom" and password is "fitz" then it exits the loop
// to the "You got it right" message.
// But shouldn't that evaluate to
// (true && false) which is false?  
// Shouldn't it go back to the beginning of the while loop?
// If someone might help me understand what I am doing wrong, I would be
// grateful.
// Thank you!!
        
            {
            cout << "Uh no... that's not quite right. \n";
            cout << "Would you like to try again?\n";
            cin >> choice;

            if (choice == "no"){
                return 0;
            }
            else {
                cout << "Enter your username\n";
                cin >> username;
                cout << "Enter your password\n";
                cin >> password;
            }
        }

       cout << "\nYou got it right.  Come on in. \n";

    // proceed to the rest of the program

    // return is a good way to terminate the program.

    return 0;
}
Think about it. If I gave my username as "mike" and my password as "pass":

- would password != "pass" be true or false?

- would username != "tom" be true or false?

- would password != "pass" || username != "tom" be true or false?

- would password != "pass" && username != "tom" be true or false?

EDIT: Oops, sorry, I messed up my original values of the username and the password. Apologies! Please use the edited versions.
Last edited on
Thank you for your reply!!

Ok - please bear with me while I work through this:

password != "pass" would be false
username != "tom" would be true

password !="pass" || username != "tom" would be
false OR true, which would be True.

Which should exit the loop, right?

password != "pass" && username != "tom" would be
false AND True, which would be False.

Which should repeat the loop, right? Because the while loop only executes until the condition is true?

I have a feeling I am missing (or misunderstanding) something fundamental here and it is just escaping me.
Looks like you misunderstand how while loops work.
They work like: while condition is true do actions
1
2
while(condintion)
    actions

So if your condition evaluates to true, body of the loop gets executed, if it false, statements after loop actions are executed.

1
2
3
// But shouldn't that evaluate to
// (true && false) which is false?  
// Shouldn't it go back to the beginning of the while loop? 
↑false means that loop is skipped and the rest of program is executed

1
2
3
4
// While password is "fitz" and username is "tom", the expression is
// ( true || false ) which should evaluate to true, correct?
// ... which should then exit the loop and display the "You got it
// right" message.  Right?  But it doesn't. 
↑True means that loop body is executed and condition is checked again
Last edited on
Yes that's where my brain took a left turn... yeesh. Time to re-read the chapter again. Thank you! NOW it makes sense. I appreciate your time!
Last edited on
Ok - so to clean this up and solidify my understanding, here we go:

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

using namespace std;

int main () {

string password="<unknown>";
string username="<unknown>";

cout << "Enter Username\n";
cin >> username;
cout << "Enter Password\n";
cin >> password;

// while ((username != "Tom") && (password != "Pass"))

// If username entered is "Tom" and Password is "Fudge", then the result
// is False and True = False and exits the loop.  This is not the desired
// result.  You want to stay in the loop until the correct username and 
// password are entered.

/* You must present a case where the condition is false 
to make it exit the loop.  Whenever it is True, it will run 
the code in the loop.*/

while (username !="Tom" || password != "Pass")

// username Tom password Fudge 
// results in False or True = True which executes the loop.
// This is the desired result.
// username Tom password Pass
// results in False or False = False which exits the loop

    {
    cout << "Not quite.\n";
    cout << "Enter Username\n";
    cin >> username;
    cout << "Enter Password\n";
    cin >> password;
}

// end of loop - exit point.

cout << "ID accepted.  Welcome.";
return 0;
}
Last edited on
Topic archived. No new replies allowed.