Looping for user attempts

I'm making a password program for my class an I'm almost done but I cannot figure out how to give the user only three failed tries.

After 3 failed attempts I want to cout that the user has been locked and the program to end. Also if the user successfully completes the password and re-entry I want the program to end after I cout the approved message.


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
int main()
{
    string password;
    bool  validUpper = false, validNumber = false, validLower = false, validLength = false, firstPass = false;

    user_prompt(password);
    validate(password, validUpper, validNumber, validLower, validLength, firstPass);
    warning(password, validUpper, validNumber, validLower, validLength, firstPass);
    confirm_valid(password, firstPass);

    return 0;
}

bool confirm_valid(string password, bool firstPass)
{
    string confirmPassword;

    if (firstPass == true)
    {
    cout << "Re-enter the password for validation:";
    cin >> confirmPassword;

    if(confirmPassword != password)
    {
        cout << endl;
        cout << "<<< This password \"" << confirmPassword << "\" does not match \"" <<
        password << "\" >>>";

        return (false);
    }
    else
    {
        cout << endl << "*** The password has been approved ***" << endl;
        return (true);
    }
    }
    return true;
}
I would use a while loop to ask the user for the three passwords (while password isn't right AND if user hasn't used his three tries already)

Break the loop if password is right or if password is wrong more than three times.

then split the code with an if else statement, if password is right, display so, if it is wrong, display so. Then end program.

Hope this helps,

Regards,

Hugo.
Last edited on
So first I'm just trying to get the program to end after a successful password and re-entry. This is what I came up. In main I made another bool variable called success and set it to false.

While success is false I loop over the functions prompting the user for password input. False entries are properly looping back over.

However on entries that succeed I set success = true. Hoping it will stop the loop but it doesn't do that. Any idea why?

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
int main()
{
    string password;
    bool  validUpper = false, validNumber = false, validLower = false, validLength = false, firstPass = false, success = false;

   while(success == false){
    user_prompt(password);
    validate(password, validUpper, validNumber, validLower, validLength, firstPass);
    warning(password, validUpper, validNumber, validLower, validLength, firstPass);
    confirm_valid(password, firstPass, success);
   }
    return 0;
}
bool confirm_valid(string password, bool firstPass, bool success)
{
    string confirmPassword;

    if (firstPass == true)
    {
    cout << "Re-enter the password for validation:";
    cin >> confirmPassword;

    if(confirmPassword != password)
    {
        cout << endl;
        cout << "<<< This password \"" << confirmPassword << "\" does not match \"" <<
        password << "\" >>>";

        success = false;
    }
    else
    {
        cout << endl << "*** The password has been approved ***" << endl;
        success = true;
    }
    }
    return success;
}
Last edited on
Yes you need to pass success by reference in your functions if you don't return them. Otherwise the bool will be changed inside the function but it's changed state will not be returned to main.

Try this:
 
bool confirm_valid(string password, bool firstPass, bool &success)


It should change success inside your confirm_valid function and its value should be changed in main too
Last edited on
One way to 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
#include <iostream>
#include <string>

using namespace std;

const int MAX_TRIES = 3;

int main()
{
  bool logged_in = false;
  int num_tries = 0;
  string password;

  while(!logged_in && num_tries < MAX_TRIES)
  {
    cout << "Enter password: ";
    cin >> password;
    num_tries++;
    if (password == "password")
    {
      logged_in = true;
      cout << "Logged in";
    }
  }
  if(!logged_in)
  {
    cout << "Sorry - no access.";
    return 1;
  }
  // user is logged in so continue
  return 0;
}
Topic archived. No new replies allowed.