Hello guys, I have been working on this code for a few hours and I almost have everything working right. This project that I am working on is basically a password creator where a person enters their password and the program checks it to make sure that it meets certain requirements (size, upper/lowercase, and number). I also have to tell the user if the password is not valid and the reason it is not valid.

My problem is that when I type in a valid password, the program says the password is valid, but also displays the error messages, which would occur if the program was invalid. Can someone take a quick look at my code and see how I can fix this to make it work correctly. I have tried a number of different things ( you will notice that I commented a few parts out ), and I am sure that its just a small thing that is causing my problem.

Thanks for the help/support

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool checkpass(string, int);

int main ()
{
    string password;
    
    cout << "Please enter a password with atleast: /n one uppercase letter, \n one lowercase letter, \n a number, \n and be at least six characters long. \n";
    cin >> password;
    cout << endl;
    
    int n = password.length();
    
    if (checkpass(password, n))
       cout << "Your password is valid";
    else
        cout << "Your password is not valid";
        
     getchar ();
     getchar ();   
        
    return 0;
    
}

bool checkpass(string pword, int length)
{
     bool size, lower, upper, number, total;
     
     if (length > 6)
        size = true;
     else
         {
             size = false;
             cout << " Your password is too short." << endl;
         }
         
     for (int i = 0; i <= length; i++)
         {    
               if (pword[i] >= 97 && pword[i] <= 122 )
                   {
                       lower = true;
                       break;
                       }
                      
               else
                   {
                       lower = false;
                       cout << "Your password needs a lower case letter" << endl;
                   }
         }
     
     for ( int j = 0; j < length; j++)
         {    
               if (pword[j] >= 65 && pword[j] <= 90 )
                   {
                       upper = true;
                       break;
                   }
                      
               else
                   {
                       upper = false;
                       cout << "Your password needs an upper case letter" << endl;
                       //break;
                   }
         }
         
for (int k = 0; k < length; k++)
         {    
               if (pword[k] >= 48 && pword[k] <= 57 )
                   {
                       number = true;
                       break;
                   }
                      
               else
                   {
                       number = false;
                       cout << "Your password needs a number" << endl;
                       //break;
                   }
         }
         
     /*if (lower = false)
        cout << "Your password needs a lower case letter" << endl;
     
     if (upper = false)
        cout << "Your password needs an upper case letter" << endl;
        
     if (number = false)
        cout << "Your password needs a number " << endl;
     
     */
         
     if ( size == lower == upper == number)
         total = true;
     else
         total = false;
         
     return total;
}

Hi theblastedfool :-)

Your problem is in the checkpass-function.

Let's take a look at the check for a lower case letter:

for ( int i = 0; i <= length; i++)
{
   if ( pword[ i] >= 97 && pword[ i] <= 122 )
   {
      lower = true;
      break;
   }
   else
   {
      lower = false;
      cout << "Your password needs a lower case letter" << endl;
   }
}

You almost got it right. The problem is that you output the error-message every time you find a letter that is not lower case.

The password "qweRty" will show the error message three times while checking the first three letters before breaking out of the loop at the fourth letter.

Use the loop to check for a lower case letter but don't show the error until after the loop.

Something like this:

lower=false;

for(int i=0; i<length; i++)
{
   if(pword[i] >= 97 && pword[i] <= 122)
   {
      lower=true;
      break;
   }
}

if(!lower)
    cout << "Your password needs a lower case letter" << endl;

Edited 6 Years Ago by mbulow: n/a

Perfect, I knew that there was a reason for that, but I was not sure how to fix it properly. Now my code is functioning properly. Thank you mbulow!

This question has already been answered. Start a new discussion instead.