password strenght determination

hey, i am writing a program that will determine the strengh of any password entered, based on a certain criteria.
i have written the code and run it, but the way i wrote it, i always get "verify1 = n, verify2= n, verify3 = n. I know it is because, for example if I enter "Manypoepl&56them" as password, the do while loop sets verify 1 to 'y' the first time, but then sets it to 'n' when it checks the second letter. I want to know if anyone has any idea on how to avoid this. The code is below;

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;


int main()
{
int count=0;
char password, verify1, verify2, verify3, verify4;


cout << "Welcome.\n" <<
"This program helps youu to determine the strength\n" <<
"of any password you may chose, based on all of the following criteria for password strength:\n" <<
"1.At least 12 total characters.\n" <<
"2.At least one digit.\n" <<
"3.At least one lowercase letter.\n" <<
"4.At least one uppercase letter.\n" <<
"5.At least one character that is neither a letter nor a number,\n" <<
"(for example, a punctuation character).\n";

cout << "Now enter your choice of password:\n";

do
{
cin.get(password);

if (isupper(password))
verify1 = 'y';
else
verify1 = 'n';

if (islower(password))
verify2 = 'y';
else
verify2 = 'n';

if (isdigit(password))
verify3 = 'y';
else
verify3 = 'n';

if (isalpha(password))
verify4 = 'y';
else
verify4 = 'n';

count ++;
}while (password != '\n');

if ((verify1 == 'y' && verify2 == 'y') && (verify3 == 'y' && verify4 == 'n') && (count >= 12))
{
{
cout << "This password looks pretty good.\n";
}
}
else
{
if (verify1 == 'n')
cout << "Your password does not have at least one uppercase letter.\n" << endl;

if (verify2 == 'n')
cout << "Your password does not have at least one lowercase letter.\n" << endl;

if (verify3 == 'n')
cout << "Your password does not have at least one digit.\n"<< endl;

if (verify4 == 'y')
cout << "Your password does not have at least character that is neither\n"
"a letter nor a number.\n" << endl;
if (count < 12)
cout << "Your password should be at least 12 characters long.\n"<< endl;
}


while ((verify1 == 'n' || verify2 == 'n') || (verify3 == 'n' || verify4 == 'y') || (count < 12))
{
cout << "Please enter another password for verification.\n";
do
{
cin.get(password);
}while (password != '\n');
}

char wait; cin >> wait;
return 0;

}


Start them all out as 'n' by default, and flip them to 'y' as soon as you find at least one of each.
i tried that, but the same thing happens again. Do you have any particluar logic of operation for the code in mind?
Okay... your code has a staggering amounts of errors.

Here's how I'd model the loop:
1
2
3
4
5
6
7
8
9
10
program beginning, with variable declarations, intro text, etc.
main loop begin
	set everything to default state
	input password
	begin loop through every character
		4 if statements, set the corresponding verify char to 'y' if each of the requirements is found
	end character loop
	output results
main loop end
return 0;

Hopefully that helps. Give it an earnest go, but if that didn't make it clear, I'll write some actual code for you.
Thanks Timaster, i changed the code to the following and it works pretty well, the only twitch is that, it cannot verify a character that is neither a letter nor number. It always seems to get one such character no matter what I put in;
Any help?


#include <iostream>
#include <cstdlib>
using namespace std;

void verification(bool& try_newpassword, char& verify1, char& verify2, char& verify3, char& verify4, char& character);
//Precondition; This function requires the user to enter any password he/she choses for verification.
//Postcondition; The function then determines the strenght of the password based on the following criteria;
//1.At least 12 total characters.
//2.At least one digit.
//3.At least one lowercase letter.
//4.At least one uppercase letter.
//5.At least one character that is neither a letter nor a number,
//(for example, a punctuation character).
//If password satisfies all criteria, the function tells the user so,
//otherwise, it tells the user what criterion is not met by the password.


int main()
{
int count=0;
char password, verify1 = 'n', verify2 = 'n', verify3 ='n', verify4 ='y', character ='n';
bool try_newpassword;

cout << "Welcome.\n" <<
"This program helps youu to determine the strength\n" <<
"of any password you may chose, based on all of the following criteria for password strength:\n" <<
"1.At least 12 total characters.\n" <<
"2.At least one digit.\n" <<
"3.At least one lowercase letter.\n" <<
"4.At least one uppercase letter.\n" <<
"5.At least one character that is neither a letter nor a number,\n" <<
"(for example, a punctuation character).\n";


cout << "Please enter your choice of password:\n";

verification(try_newpassword, verify1, verify2, verify3, verify4, character);

while (try_newpassword == true)
{
cout << "Please enter another password for verification.\n";

verification(try_newpassword, verify1, verify2, verify3, verify4, character);
}


char wait; cin >> wait;
return 0;

}


void verification(bool& try_newpassword, char& verify1, char& verify2, char& verify3, char& verify4, char& character)
{
char password;
int count;
do
{
cin.get(password);

if (isupper(password))
verify1 = 'y';

if (islower(password))
verify2 = 'y';

if (isdigit(password))
verify3 = 'y';

if (isalpha(password))
verify4 = 'y';
else
character = 'y';

count ++;
}while (password != '\n');

if ((verify1 == 'y' && verify2 == 'y') && (verify3 == 'y' && character == 'y') && (count >= 12))
{
{
cout << "This password looks pretty good.\n";
}
try_newpassword = false;
}
else
{
if (verify1 == 'n')
cout << "Your password does not have at least one uppercase letter.\n" << endl;

if (verify2 == 'n')
cout << "Your password does not have at least one lowercase letter.\n" << endl;

if (verify3 == 'n')
cout << "Your password does not have at least one digit.\n"<< endl;

if (character == 'n')
cout << "Your password does not have at least character that is neither\n"
"a letter nor a number.\n" << endl;
if (count < 12)
cout << "Your password should be at least 12 characters long.\n"<< endl;

try_newpassword = true;


}

return;
}
Last edited on
What you'll want to do for that is use isalpha() and isdigit() on every character, and set the flag to 'y' if it is neither. Use the logical and (&&), or (||), and not (!) operators (you'll need two).
For example: !(isalpha() || !isdigit()) is true when isalpha() is false and isdigit() is true.
Again, let me know if I'm just not making any sense, and I'll write you some actual code :)
Thanks again Timaster, I tried to use the logical statements above , but the results are the same as before, everything works, except the checking of the non-letter, non-digit character. Sorry to bother you again, but I think I might have to take you up on your offer and ask for some actual code. :)
Thanks.
You could do it two ways, with an or, checking whether it is not an alpha or a digit, or you could check if it isn't an alpha and isn't a digit.
Yep. And since I promised...

1
2
if (!isalpha(password)&&!isdigit(password))
	verify4 = 'y';

or
1
2
if (!(isalpha(password)||isdigit(password)))
	verify4 = 'y';

To me, it makes sense in English, as firedraco wrote it, but you can also prove it by looking through it and making sure that the result is what we want.
I'll post a table in a few minutes... have to go somewhere at the moment.
Last edited on
thanks firedraco. Timaster, thanks again. I looked at ur code and found my source of errorr. Seems like I was using a logical || instead of &&, so I was getting a true, even when one logic was false. I had completely missed it. Thanks again. It works perfectly now.
thanks firedraco. Timaster, thanks again. I looked at ur code and found my source of errorr. Seems like I was using a logical || instead of &&, so I was getting a true, even when one logic was false. I had completely missed it. Thanks again. It works perfectly now.
Oh good. I was about to post a table, but if you got it, cool :)
Topic archived. No new replies allowed.