Hi, I am supose to do a program in C++ that tests for a password
it should do the following:

a- the password should be between 6 and nine characters
b- the password should contain at least one upper case and at least one lowercase letter
c- the password should contain at least one digit

the program works fine to check for requirements b and c but it does not work properly when I check the string length here is the code I got so far:
Thanks is advance :)

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

bool testlower( char [], int);
bool testupper( char[], int);
bool testdigit( char[],int);
bool testnum(char[]);



int main()
{
	int const size = 10;  //can handle 9 characters 
	char password[size];


	cout <<" Enter your password:" <<endl; 
	cout <<" Make sure that its more than six characters\n";
	cout <<" and its less than 9 characters long.\n";
	cout <<" has at least one digit and \n";
	cout <<" at least one uppercase and lower case letter\n";
	cout <<" otherwise the program wont work proberly, thank you for you cooporation\n";
	cin.getline(password,size);

	
	if ((testlower( password, size) && testupper (password,size) && testdigit ( password,size)&& testnum(password))== true)
	cout <<"the password that you entered is valid\n";
	

	if ( testlower( password,size) == false )
	{
		cout<<" the password that you entered is invalid because there is\n";
		cout<<" no lower case letter in your password.\n";
	}
	 if( testupper( password, size) == false)
	 {
		 cout<<" the password that you entered is invalid because there is\n";
		 cout<<" no upper case letter in your password.\n";
	 }
	 if ( testdigit(password,size) == false)
	 {
		 cout<<" the password that you enterd is invalid because there is\n";
		 cout<<" not digits in your password\n";
	 }
	 if (testnum(password)== false)
	 {
		 cout<<" the password that you entered is invalid because the number\n";
		 cout<<" of characters is less than 6 or is more than 9 \n";
	 }
	
	return 0;

	
}
//-------------------------------------------------------

bool testlower(char password[], int size)
{
	for ( int count=0; count < (size-1); count++)
	{
		if( islower(password[count]))
		
			return true;
		
		
	}
	return false;
}
//--------------------------------------------------------

bool testupper( char password[], int size)
{
	for ( int count=0; count < (size-1); count++)
	{
		if ( isupper( password[count]))
		
			return true;
	
		
	}
return false;
}
//-------------------------------------------------------
bool testdigit( char password[], int size)
{
	for ( int count=0; count < (size-1); count++)
	{
		if ( isdigit(password[count]))
		
			return true;
		
		
	}
	
	return false;
}
//this is where the code in not working properly I tried to modify it in many ways
//but still can't get the right asnwer

bool testnum( char password[])
{	
	int length;
	length = strlen(password); 
	
  if( length < 6 || length <=9)
  {
	  return false;
  }

  return true;
}

Recommended Answers

All 3 Replies

>>but it does not work properly when I check the string length
Just add code to call strlen() to get the password length the check if it is <= 9. If you really wrote all that code you posted you should have no problem checking for length.

Another thing to consider, what if the user enters 15 characters? All you read are the first 9 and he thinks his password is longer than it is. You should probably accept more characters and give an error if 10 or more are entered.

length < 6

I read length smaller than 6. I don't think you want that

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.