If the first letter of my password is capital it passes the "At least one capital in password" requirement.
But if any other letter in the password is capital beyond the first one, the loop does not see it and tells me I need at least one capital letter for the password to be valid.
void check_valid_password()
{
char string[16]; //Checking the password for each condition.
int check4cap;
do{
cout << "Enter the password you would like to use,\n"
<< "it must conain atleast 5 letters, start with\n"
<< "a letter, contain atleast one capital letter\n"
<< "and have no punctuation: ";
cin.getline (string,16);
if (strlen(string) < 5) //Length
{
cout << "Your password is too short! Must be atlest 5 characters.\n\n";
}
elseif (!isalpha(string[0])) //1st Character
{cout << "The first character of your password must be a letter.\n\n";}
for(int check4cap = 0; check4cap < strlen(string); check4cap++) //for loop only checks if the FIRST value is capital!
{
if (!islower(string[check4cap]))
{
break;
} elseif (check4cap = strlen(string))
{
cout << "Your password must contain atleast one uppercase letter.\n\n";
}
}
}
while (strlen(string) < 5 || !isalpha(string[0]));
system("pause");
}
bool bHasCapital = false;
// Don't declare another check4cap here
for(check4cap = 0; check4cap < strlen(string); check4cap++) //for loop only checks if the FIRST value is capital!
{
if (!islower(string[check4cap]))
{
bHasCapital = true;
break;
}
}
if(!bHasCapital) cout << "Your password must contain atleast one uppercase letter.\n\n";