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

May 17, 2010 at 9:14am
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 May 17, 2010 at 9:16am
May 17, 2010 at 10:11am
try changing check4cap = strlen(string) in check4cap == strlen(string)
May 17, 2010 at 7:21pm
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 May 17, 2010 at 7:34pm
May 17, 2010 at 8:28pm
Your else if never is reached, the condition on your for loop causes it to exit when check4cap == strlen(string).
May 17, 2010 at 8:39pm
So would this still work then if instead I do this?

for(int check4cap = 0; check4cap <= strlen(string); check4cap++)
Last edited on May 17, 2010 at 8:39pm
May 17, 2010 at 11:50pm
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 May 17, 2010 at 11:51pm
May 18, 2010 at 12:54am
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.