Question

I'm trying out an exercise where I have the user write a pass and I have to check the pass for length (must be at least 6 characters), upper case, lower case, and at least 1 number. for some reason, when I try to make it work, it wont detect both upper case and lowercase. even when "lower" or "upper" are == true, it still goes to the nested if (!lower) or if (!upper) conditional and runs that code block when the other is false.

can anyone show me where the logic is wrong? I'm not understanding why its even accessing an if with it false if the bool variable is set to true.

 bool verified(const char *pass)
 {
     int length = strlen(pass);
     bool lower, upper, number;
     lower = hasLower(pass);
     upper = hasUpper(pass);
     number = hasNumber(pass);
     if (length < 6)
     {
         cout << "INVALID: Your pass does not contain enough characters";
         return false;
     }
     else
     {
         if (lower && upper && number)
         {
             cout << "Your pass is valid and verified. Congrats!";
             return true;
         }
         if (!lower)
         {
             if (!number)
             {
                 cout << "INVALID: Your pass does not contain lower case letters or at least 1 number";
                 return false;
             }
             if (!upper);
             {
                 cout << "INVALID: Your pass does not contain lower case or upper case letters";
                 return false;
             }
             cout << "INVALID: Your pass does not contain lower case letters";
             return false;
         }
         if (!upper);
         {
             if (!number)
             {
                 cout << "INVALID: Your pass does not contain upper case letters or at least 1 number";
                 return false;
             }
             if (!lower);
             {
                cout << "INVALID: Your pass does not contain upper case or lower case letters"1;
                return false;
             }
             cout << "INVALID: Your pass does not contain upper case letters";
             return false;
         }
         if (!number)
         {
             cout << "INVALID: Your pass does not contain at least 1 number";
             return false;
         }
     }
 }

method for checking upper case

 bool hasUpper(const char *pass)
 {
     bool upper;
     for (int i = 0; i < strlen(pass); i++)
     {
         upper = isupper(pass[i]);
         if (upper == true)
         {
             return true;
             break;
         }
     }
     return false;
 }

method for checking lower case

 bool hasLower(const char *pass)
 {
     bool lower;
     for (int i = 0; i < strlen(pass); i++)
     {
         lower = islower(pass[i]);
         if (lower == true)
         {
             return true;
             break;
         }
     }
     return false;
 }

method for checking for number

 bool hasNumber(string pass)
 {
     if (pass.find('0') != string::npos ||
         pass.find('1') != string::npos ||
         pass.find('2') != string::npos || 
         pass.find('3') != string::npos || 
         pass.find('4') != string::npos || 
         pass.find('5') != string::npos || 
         pass.find('6') != string::npos ||
         pass.find('7') != string::npos || 
         pass.find('8') != string::npos ||
         pass.find('9') != string::npos)
         return true;
     else
         return false;
 }
Was it helpful?

Solution

You have written this:

if (!upper) *;*

You see the ; after it??

Infact, you have done this in multiple locations in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top