Why doesn't this for loop work? Checking for uppercase character in password.

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.


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
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";
                  }
                  else if (!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;
                       } else if (check4cap = strlen(string))
                               {
                               cout << "Your password must contain atleast one uppercase letter.\n\n";
                               }
               }
        }
          while (strlen(string) < 5 || !isalpha(string[0]));
     
     
     system("pause");
}
Last edited on
try changing check4cap = strlen(string) in check4cap == strlen(string)
Darn if I do that then it doesn't check for any capitals, if I type a password with 5 lowercase letters it lets me pass.



But actually I think it should be == anyways , I don't know why it worked before at all.
Last edited on
Your else if never is reached, the condition on your for loop causes it to exit when check4cap == strlen(string).
So would this still work then if instead I do this?

for(int check4cap = 0; check4cap <= strlen(string); check4cap++)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
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";
Last edited on
Nice , thanks for all the help, in class today my teacher helped me solve it using a bool like shown above.

Thanks.
Topic archived. No new replies allowed.