I can't figure out how to add a loop to allow the user only three attempts to enter in a valid password. Any hlp would be appreciated it .

#include <iostream> // cin and cout
#include <cstring> // for string
#include <conio.h> // for getch
#include <cctype> // conversion
using namespace std;

//Function Prototype
bool testPass(char []);

int main()
    char *password; //dynamically allocating an array
    int length; //make sure password is same as length
    int numCharacters; //hold number of characters for password

//get the password length from the user
    cout << "How many characters would you like your password to be?.";
    cout << " Your password must be at least 8 characters long." << endl;
    cin >> numCharacters;

    while (numCharacters < 8)
        cout << "Please enter a password length of at least 8 characters." << endl;
        cin >> numCharacters;

//dynamically allocate the array for the password
    password = new char[numCharacters+1];

    cout << "Please enter a password that contains at least one uppercase letter, ";
    cout << "one\nlowercase letter, and at least one digit." << endl;

//get users password
    cin >> password;

//Convert array to integer
    length = strlen(password);

//Check to see if password meets the lenght set by user.

    while (length != numCharacters)
        cout << "Your password is not the size you requested. ";
        cout << "Please re-enter your password." << endl;
        cin >> password;
        length = strlen(password);

         if (testPass(password))
              cout << "Your password is valid. ";
             cout <<"Password invalid" << endl;
             cin >> password;

//*This function will verify to make sure the password contains
//atleast one upper and lowercase and one digit.

bool testPass(char pass[])
    // flags
    bool aUpper = false,
         aLower = false,
         aDigit = false ;
    for ( int index = 0 ; pass[index] ; ++index )
        if ( isupper(pass[index]) )
            aUpper = true ;
        else if ( islower(pass[index]) )
            aLower = true ;
        else if ( isdigit(pass[index]) )
            aDigit = true ;
    if ( aUpper && aLower && aDigit )
        return true;
        return false ;
3 Years
Discussion Span
Last Post by richieking

online 43. set up a static int counter =0;

then increment the counter in the while loop . check with if the counter is equall 3 break or else keep asking for the password to be entered correctly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.