Hi, this program verify password inputs.
The password should contain at least a lowercase and an upper case letter and a number.
But my code has bugs.
It still shows password is valid when I entered password "helen123" which does not have upper case letter.
Can you guys please help?
See the proof at the bottom.
#include <iostream>
#include <cstring>
using namespace std;
//function prototypes
bool verifyPassword(char *);
int verifyUpper(char *);
int verifyLower(char *);
int verifyNum(char *);
int verifyAlpha(char *);
int main()
{
const int SIZE=100; // array size
char password [SIZE]; // to hold a password string
// get the password input from users
cout << "The password must have 6 or more characters: " << "\n";
cout << "at least one uppercase character, " << "\n";
cout << "at least one lowercase character, and" << "\n";
cout << "at least one numeric digit. " << "\n";
cout << "\n";
cout << "Enter your password : ";
cin.getline (password, SIZE);
int length;
length = strlen(password);
if(length >= 6&&length<=SIZE)
{
while (!verifyPassword(password))
{
cout << "Invalid Password. Please enter VALID password : ";
cin.getline (password, SIZE);
}
cout << "Your password: " << password << ", is valid\n";
}
else
{
cout << "The password must have at least 6 or more characters long\n";
//verify upper case contained
int verifyUpper(char *str)
{
int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isupper(str[count]))
num++;
}
return num;
}
//verify lower case contained
int verifyLower(char *str)
{
int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!islower(str[count]))
num++;
}
return num;
}
//verify number contained
int verifyNum(char *str)
{ int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isdigit(str[count]))
num++;
}
return num;
}
//verify if inputs are all alphabetic
int verifyAlpha(char *str)
{ int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isalpha(str[count]))
num++;
}
return num;
}
/*
The password must have 6 or more characters:
at least one uppercase character,
at least one lowercase character, and
at least one numeric digit.
Enter your password : helenhan
Invalid Password. Please enter VALID password : 1234567
Invalid Password. Please enter VALID password : HELENHAN
Invalid Password. Please enter VALID password : helenHan
Invalid Password. Please enter VALID password : helen123
Your password: helen123, is valid
Press any key to continue . . .
I don't a compiler with me to check, but maybe it thinks the numbers are upper case? Can you alter your test to ignore number when testing the case?
By the way, you code is rather more complicated than it needs to be to solve the stated problem. For example, you only need to know there's at least one number. You don't need to count them.
And you should be able to roll all checks into a single loop.
You are testing the wrong way. For example, here is your verifyUpper() function:
1 2 3 4 5 6 7 8 9 10 11
int verifyUpper(char *str)
{
int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isupper(str[count]))
num++;
}
return num;
}
This code will return zero of all the letters are uppercase, and non-zero otherwise. (Remember that a non-zero is the same as true, while zero is the same as false.)
Consider what is happening on line 7. If it is not uppercase, you increment num (that is, you change any false to true).
Once you have that figured out, consider this: do you really need to check every character in your string? As long as you have at least one uppercase character, you know you can return true:
1 2 3 4 5 6 7 8 9 10
// pseudo code
bool validateUpper(...)
{
for (...)
{
if (found an uppercase)
return yes, contains an uppercase letter
}
return no, no uppercase letter was found
}