Link to home
Start Free TrialLog in
Avatar of kennyu125
kennyu125

asked on

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;
}
Avatar of efn
efn

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.
ASKER CERTIFIED SOLUTION
Avatar of vinquus
vinquus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of kennyu125

ASKER

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?
Can you show us the function as it is now?  Did you change it?  Did you get rid of the "return false" statement?
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

SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial