I was assigned to write a program that will verify a password entered by the user. The program should verify that the password has 6-10 characters and atleast 1 uppercase, 1 lowercase, and 1 numeric digit. The program runs, but will only display the length of the password entered and won't verify any of the input.

I'm completely lost on how to get my verifyPass() function to work properly and am probably taking the wrong approach to verifying. Please help!

here's the code

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

using namespace std;

//function prototypes
bool verifyPass(char []);

const int LENGTH = 11;
int main()
{
    
    
    char password[LENGTH];
    
    cout << "Enter a password with the following criteria:\n";
    cout << "6-10 characters\n";
    cout << "1 uppercase\n1 lowercase\n1 numeric digit\n";
    cout << "(ex. xxXx1x, Aaaaa2): "; 
    cin.getline(password, LENGTH);
    
    
        if(verifyPass(password))
        cout << "That is a valid password\n";
        else 
        cout << "That is an invalid password\n";

    system("pause");
    return 0;   
}

bool verifyPass(char pw[])
{
    int length;
    length = strlen(pw);
    if(length >= 6 && length <=10)
    {
        for (int count = 0; count < LENGTH; count++)
        {
            while (!islower(pw[count]))
            return false;
        }
        for (int count = 0; count < LENGTH; count++)
        {
            while (!isdigit(pw[count]))
            return false;
        }
        for (int count = 0; count < LENGTH; count++)
        {
            while (!isupper(pw[count]))
            return false;
        }
    }    
    else
    cout << "Must enter a password 6-10 characters long\n";
    
    return true;   
}

So what you should do is, make more function, specifically these :

bool hasUpperCase(const char * str); //returns true if str has at least 1 upper case 
bool hasLowerCase(const char * str); //returns true if str has at least 1 lowercase 
bool hasDigit(const char * str);//returns true if str has at least 1 digit
bool isInvalidLength(int len); //returns true if len is invalid length

and call those functions inside your verifyPass function like so :

bool verifyPass(const char *str){
 int len = strlen(str);
 return hasUpperCase(str) && hasLowerCase(str) && !isInvalidLength(len);
}

Now your job is to code the hasUpperCase, hasLowerCase and isInvalidLength function.

Looping over the string keep in mind that the code is looking for a single instance of the character type. Avoid breaking the loop until the end. Your code does not work because the first loop in verifyPass returns false if *any* of the characters in the whole string is not lower case.

One loop can be used with flags and then the flags are tested after the loop covers the whole string.

bool verify_pass(string const& pw) {
    bool length = pw.size() >= 6 && pw.size() <= 10;
    bool upper = false, lower = false, digit = false;
    if (length) {
        for (auto x = pw.begin(); x != pw.end(); ++x) {
            if (isupper(*x))
                upper = true;
            else if (islower(*x))
                lower = true;
            else if (isdigit(*x))
                digit = true;
        }
    }
    return length && upper && lower && digit;
}

This function works for a whole string test, but more detail for the reason why the stirng failed is good idea. That is an exercise for you now that the problem is solved. :)

p.s. It is not a good idea to limit the upper length of the string because longer passwords are more likely to be secure than shorter passwords. A lower limit is good.

>>This function works for a whole string test

Sure, the only problem is that most compiler right now do not support the auto keyword,
unless you got the very recent ones, which I doubt OP has. Although I might be wrong.

Sure, the only problem is that most compiler right now do not support the auto keyword

This is not a problem with the code. This is a problem only if the code is cut and pasted without understanding how it works. That is always a bad idea. :) And the best way to promote C++0x is to use it and teach it, yes? That is progress.

>>the best way to promote C++0x is to use it and teach it, yes

Lol, yes. But lets leave that to the C++0x forum.

So what you should do is, make more function, specifically these :

bool hasUpperCase(const char * str); //returns true if str has at least 1 upper case 
bool hasLowerCase(const char * str); //returns true if str has at least 1 lowercase 
bool hasDigit(const char * str);//returns true if str has at least 1 digit
bool isInvalidLength(int len); //returns true if len is invalid length

and call those functions inside your verifyPass function like so :

bool verifyPass(const char *str){
 int len = strlen(str);
 return hasUpperCase(str) && hasLowerCase(str) && !isInvalidLength(len);
}

Now your job is to code the hasUpperCase, hasLowerCase and isInvalidLength function.

Oh wow, I didn't even think to break the isupper, islower, isdigit into seperate functions. I'll try it. Thanks!

>>Oh wow, I didn't even think to break the isupper, islower, isdigit into seperate functions. I'll try it. Thanks

No problem. The more function you create and thus divide the program, the better.
That is because of re-usability. So in later on the code, if you want to use the
hasUpperCase function then you can just call it, instead of copying and pasting code.

Using functions makes your code more readable and cleaner and better maintainable, so
the more the merrier.

ok, so i've changed some things up and the program is verifying password length, upper and lower. The only thing now is that it does accept passwords with numbers which is what it is supposed to do, but it also accepts without numbers as well.

Can I get a tip on whats wrong with my verifyNum() function.

#include <iostream>
#include <cstring>
using namespace std;

//function prototypes
bool verifyPass(char *);
int verifyUpper(char *);
int verifyLower(char *);
int verifyNum(char *);

int main()
{   
	char password[100];    
	cout << "Enter a password with the following criteria:\n";
    cout << "6 to 10 characters\n";
    cout << "1 uppercase\n1 lowercase\n1 number:\n";
	cin >> password; 
    
    int length;
    length = strlen(password);
    
	if(length >= 6 && length <= 10)
    {
        while (!verifyPass(password))    
	   {        
		  cout << "Invalid Password. try again\n";        
		  cin >> password;    
	   }    
	   cout << "Your password: " << password << ", is valid\n";    
    }
    else
        cout << "The password entered must be between 6-10 characters long\n";            
	    
	system("pause");    
	return 0;
}

//function definitions
bool verifyPass(char *str)
{    
	int len = strlen(str); 
    return verifyUpper(str) && verifyLower(str) && verifyNum(str);
}
//count
int verifyUpper(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!isupper(str[count]))
		num++;
    }
		return num;    
}
int verifyLower(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!islower(str[count]))
		num++;
	}
	return num;
}
int verifyNum(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!isdigit(str[count]))
        num++;
	}
		return num;
}

You could add another function if(isalpha(str[count]))
it checks if all input is alphabet, it is invalid...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.