Password requirement verifier

This program is suppose to make sure that what ever password is typed in, meets the requirements of the password. The requirements are that it must have one uppercase letter, one lower case letter, and at least one digit. The program runs, but it keeps saying that the password you entered is invalid. Please help me fix this. Thank you.
Here is the code:
#include <iostream>
#include <cstring>
using namespace std;

int countNumChars(char pass[])
{
      int num = 0;
      int len = strlen(pass);

      for (int count = 0; count < len; count++)
      {
            if (!isdigit(pass[count]))
            {
                  return false;
            }
            else
            {
                  num++;
            }

      //// TODO: add code for this function////
            }
            return num;
}

int countLowerChars(char pass[])
{
      int numa = 0;
      int lena = strlen(pass);

      for (int counta = 0; counta < lena; counta++)
      {
            if (!islower(pass[counta]))
            {
                  return false;
            }
            else
            {
                  numa++;
            }
      //// TODO: add code for this function////
      }
      return numa;
}

int countUpperChars(char pass[])
{
      int numb = 0;
      int lenb = strlen(pass);

      for (int countb = 0; countb < lenb; countb++)
      {
            if (!isupper(pass[countb]))
            {
                  return false;
            }
            else
            {
                  numb++;
            }
      }
            return numb;
}

bool isValidPassword(char *password)
{    
      int len, numberChars, lowerCaseChars, upperCaseChars;    
      bool result = false;    
      len = strlen(password);    
      numberChars = countNumChars(password);    
      lowerCaseChars = countLowerChars(password);    
      upperCaseChars = countUpperChars(password);    
      result = ((len>=6)&&(lowerCaseChars>=1)&&(upperCaseChars>=1)&&(numberChars>=1));    
      return result;
}
int main()
{    
      char pass[100];    
      cout << "Please enter a password.\n";    
      cout << "You must make sure your password has at\n";    
      cout << "least one uppercase and at least one\n";    
      cout << "lowercase letter and at least 1 number.\n";  
      cin >> pass;  
      while (!isValidPassword(pass))    
      {        
            cout << "Invalid Password. Please type again.\n";        
            cin >> pass;    
      }    
      cout << "Your password: " << pass << ", is good and accepted.\n";    
      
      system("pause");    
      return 0;
}
kennyu125Asked:
Who is Participating?
 
vinquusConnect With a Mentor Commented:
firstly, countNumChars() will not work as it has the line:

if (!isdigit(pass[count]))
{
return false;
}

i.e. if any letter is not a digit, return false. You should not return false when you mean to return an int, and also, false translates to zero, so a valid character will return a char count of zero.

Secondly, in countLowerChars, the lines:

            if (!islower(pass[counta]))
            {
                  return false;
            }
            else
            {
                  numa++;
            }

this will return zero if any character is not lowercase, instead of counting the lower case characters. Delete the lines {, return false, } and else to fix it. Do the same with the countUpperChars function and it should work.
0
 
efnCommented:
All of your counting functions return false, which turns into zero, as soon as they see the first character they don't want to count.  This interferes with their function of delivering accurate counts.  If you are counting characters of some kind and you see a character of a different kind, returning false is not the thing to do.
0
 
ewest02Commented:
A quick look shows that each scan for uppercase, lowercase or digit characters may end prematurely.

consider:

 for (int countb = 0; countb < lenb; countb++)
      {
            if (!isupper(pass[countb]))
            {
                  return false;
            }

If your password is "123XYz", this for loop will terminate on the first character "1".  Instead, you need to parse the entire string.  Similar comments apply to each of your parsing functions.

Minor point... Your declare your functions to return int, but you are returning false or a count depending on the code flow. Be consistent. Return values should be of the declared return type.

  --Eric

 --Eric
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
kennyu125Author Commented:
The program works great with out the function countNumChars() , thank you for all of your help,but i still can't figure out how to work the countNumChars()  function. Once i put the function back in, the program goes crazy.
For example i input the password: 4hdijk (It is six characters because the program requires it to be or else the password will be invalid.) , but the output still says that it is valid even though there are no uppercase letters. Likewise if i input: jJdH5y, the program outputs that it is not a valid password even though it meets all three criteria.
My question is how would I make the function countNumChars()  not return false for every non-digit character it encounters? How would i make it 'skip' those characters until it finds a digit?
0
 
efnCommented:
Can you show us the function as it is now?  Did you change it?  Did you get rid of the "return false" statement?
0
 
kennyu125Author Commented:
Yes, I did. Here is the modified code. I tried to delete the lines {, return false, } and else from the function countNumChars like it did for the fuctions countLowerChars and countUpperChars with no luck.
#include <iostream>
#include <cstring>
using namespace std;
 
int countNumChars(char pass[])
{
	int num = 0;
	int len = strlen(pass);
 
	for (int count = 0; count < len; count++)
	{
		if (!isdigit(pass[count]))
		{
			return void;
		}
		else
		{
			num++;
		}
 
	
	}
		return num;
}*/
 
int countLowerChars(char pass[])
{
	int numa = 0;
	int lena = strlen(pass);
 
	for (int counta = 0; counta < lena; counta++)
	{
		if (!islower(pass[counta]))
		
			numa++;
		
	
	}
	return numa;
}
 
 
 
int countUpperChars(char pass[])
{
	int numb = 0;
	int lenb = strlen(pass);
 
	for (int countb = 0; countb < lenb; countb++)
	{
		if (!isupper(pass[countb]))
		
			numb++;
		
	}
		return numb;
}
 
bool isValidPassword(char *password)
{    
	int len, numberChars, lowerCaseChars, upperCaseChars;    
	bool result = false;    
	len = strlen(password);    
	lowerCaseChars = countLowerChars(password);  
	upperCaseChars = countUpperChars(password);      
	/numberChars = countNumChars(password);  
	result = ((numberChars>=1)&&(lowerCaseChars>=1)&&(upperCaseChars>=1)&&(len>=6));  
	return result;
}
 
int main()
{    
	char pass[100];    
	cout << "Please enter a password.\n";    
	cout << "You must make sure your password has at\n";    
	cout << "least one uppercase and at least one\n";    
	cout << "lowercase letter and at least 1 number.\n";   
	cin >> pass;   
	while (!isValidPassword(pass))    
	{        
		cout << "Invalid Password. Please type again.\n";        
		cin >> pass;    
	}    
	cout << "Your password: " << pass << ", is good and accepted.\n";    
	
	system("pause");    
	return 0;
}

Open in new window

0
 
Infinity08Connect With a Mentor Commented:
>>                 if (!isdigit(pass[count]))
>>                 {
>>                         return void;
>>                 }

If you're counting digits, you should simply ignore the non-digit characters. Just remove that return statement. Note that returing void is definitely wrong anyway ;)

The same has to be done for the other count functions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.