• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2018
  • Last Modified:

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;
}
0
kennyu125
Asked:
kennyu125
2 Solutions
 
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
 
vinquusCommented:
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
Infinity08Commented:
>>                 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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now