Problem with If and bool

I used a bool to calculate if logging in is true, so instead of typing the calculation out i wanted to use bool "Testing" and it works if you log in on the first go, but if you fail the log in it prompts you to log in again, then it checks the bool to see if its true but it doesnt and always executes the else near line 32, I cant figure it out, if i remove the bool and type the calculation it runs fine
to login use 1 and 4 but fail it to get the second log in screen

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

using namespace std;

int main()
{
    string User = "<Unknown>";
    string Pass = "<Unknown>";
    cout << "Hello, Please enter your username: " << endl;
    getline(cin, User, '\n');
    cout << "Greetings " + User + ", How nice to see you!" << endl;
    cout << "Enter your password..." << endl;
    getline(cin, Pass, '\n');

    bool Testing = ((User == "1" && Pass == "4") || (User == "2" && Pass == "5") || (User == "3" && Pass == "6"));

    if ( Testing )
    {
            cout << "Access granted!" << endl;
            cin.get();
            return 0;
    }
    else
    {
    cout << "Invalid password or username. Please try again." << endl;
    cout << "Re-enter your username: " << endl;
    getline(cin, User, '\n');
    cout << "Enter your password..." << endl;
    getline(cin, Pass, '\n');

    if ( Testing ) // THIS IS THE PROBLEM, It always skipps and does the else
    {
            cout << "Access granted!" << endl;
            cin.get();
            return 0;
    }
    else
    {
            cout << "Access Denied! It's a shame really..." << endl;
            cin.ignore();
    }
    }
    return 0;
}
You don't ever update the value of Testing. When you get to line 32, the value of Testing is the same is it was when you set it at line 16.

And if you've gotten to line 32, then it must be false, because that line of code only executes if it was false at line 18.

Edit: And I strongly advise you to get into the habit of using a consistent indentation style. You'll find it will help you see and understand what your code's doing a lot more easily.
Last edited on
Hi x123xx,

You don't re-test the value of Testing before line 32 , that is, it is still false from the bad login attempt.

Also, I am not keen on your indentation - it doesn't show the way the if statements are nested.

This is how code::blocks formats it:

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if ( Testing ) {
    cout << "Access granted!" << endl;
    cin.get();
    return 0;
} else {
    cout << "Invalid password or username. Please try again." << endl;
    cout << "Re-enter your username: " << endl;
    getline( cin, User, '\n' );
    cout << "Enter your password..." << endl;
    getline( cin, Pass, '\n' );

    if ( Testing ) { // THIS IS THE PROBLEM, It always skipps and does the else
        cout << "Access granted!" << endl;
        cin.get();
        return 0;
    } else {
        cout << "Access Denied! It's a shame really..." << endl;
        cin.ignore();
    }
}
return 0;
}


Also consider using some functions - you already have some repeated code.

Good luck !!

EDIT: I meant reset the value, not re-test.
Last edited on
Thank you, I am on my second day of self-learning C++, How would you re test the bool?
and by using functions do you mean like this?
1
2
3
4
int Example = (
cout << "Access Granted";
cin.get();
);


or am I missing the point...
Last edited on
Thank you, I am on my second day of self-learning C++, How would you re test the bool?

After the user has re-entered the password, you need to repeat the check for whether it's valid or not.

and by using functions do you mean like this?

That's not the correct syntax for defining a function. You should have another look at how your textbook does it.
Last edited on
Topic archived. No new replies allowed.