Reputation: 3
As the title states, the program is for users to input a password. The program checks four rules for the password to pass (1). It then prompts the user to enter the password again for validation, which the program then compares to the initial password (2) and states if it the two passwords match and are acceptable.
(1) - The first rule, checking the string length (must be at least 8 characters long), is where the loop appears to state it failed regardless of if it did or not. That said, if it DID actually fail, the program makes the user enter in the password until the user enters in a password that passes this rule.
Example: If I enter in "password", it'll output the prompt that it was entered incorrectly, but will continue with the rest of the program if you enter it correctly a second time.
(2) I can't seem to correctly compare the two strings to check for their accuracy and have tried several techniques to see if it would change. Many ways resulted in the while loop cancelling out entirely.
Here is the code: (I appreciate any and all help that comes my way, been working on this program for a long time now, to no avail)
/*This program determines if the entered password is valid and if the second password entry matches the first.
*/
#include <iostream>
#include <cstring>
#include <string.h>
#include <string>
using namespace std;
void UserInput(string password);
short PasswordAlphaNumCheck(string password);
void PasswordValidationOutput(string password);
void PasswordMatch(string password, string second_password);
int main()
{
string password;
string second_password;
UserInput(password);
PasswordValidationOutput(password);
PasswordMatch(password, second_password);
return 0;
}
void UserInput(string password)
{
cout << "The Password that you are entering must: " << endl;
cout << " " << endl;
cout << "1) Be at least 8 characters long" << endl;
cout << "2) Contain at least one number" << endl;
cout << "3) Contain at least one upper case letter" << endl;
cout << "4) Contain at least one lower case letter" << endl;
cout << " " << endl;
cout << "Please enter a password now: " << endl;
cin >> password;
}
short PasswordAlphaNumCheck(string password)
{
short flag[3] = {0};
for(int i = 0; password[i] != '\0'; i++)
{
if(isdigit(password[i]))
flag[0] = 1;
if(isupper(password[i]))
flag[1] = 2;
if(islower(password[i]))
flag[2] = 4;
}
return (flag[0] + flag[1] + flag[2]);
}
void PasswordValidationOutput(string password)
{
short test = PasswordAlphaNumCheck(password);
while ((password.length() < 8) && (test < 7))
{
while (password.length() < 8)
{
cout << " " << endl;
cout << "Your password is not the correct size." << endl;
cout << "Please re-enter your password." << endl;
cin >> password;
}
cout << " " << endl;
test = PasswordAlphaNumCheck(password);
cout << password
<< " is "
<< (test == 0 ? "not supposed to cause this error. Report to inept computer engineer." :
test == 1 ? "not valid because you are missing an upper and lower case letter." :
test == 2 ? "not valid because you are missing an lower case letter and a number." :
test == 3 ? "not valid because you are missing a lower case letter." :
test == 4 ? "not valid because you are missing an upper case letter and a number" :
test == 5 ? "not valid because you are missing an upper case letter." :
test == 6 ? "not valid because you are missing a number." : "valid") << endl;
while (test < 7)
{
cout << " " << endl;
cout << "Please re-enter your password." << endl;
cin >> password;
}
}
cout << " " << endl;
}
void PasswordMatch(string password, string second_password)
{
cout << "Please enter your password again, for verification" << endl;
cin >> second_password;
while (!password.compare(second_password))
{
cout << "Your passwords do not match." << endl;
cout << " " << endl;
cout << "Please re-enter your password." << endl;
cin >> second_password;
}
cout << "Your password has been approved!" << endl;
}
Upvotes: 0
Views: 1654
Reputation: 362
Your user input function passes the password parameter by value, so it is not going to change the password variable outside the function. So when you password gets passed into your validation function, it still has the default empty value.
If you want your input function to modify the password value, you should pass by reference.
void UserInput(string& password);
Your validation function is also intended to change the value of the password variable so it should also take its parameter by reference.
void PasswordValidationOutput(string& password);
Your second_password variable has no purpose outside the PasswordMatch function, so I would suggest you change the signature of that function to:
bool PasswordMatch(string password);
Inside this function, you can declare string second_password
right before you read into it.
One last thing. In your first loop inside PasswordAlphaNumCheck, you should change password[i] != '\0'
to i != password.length()
. Checking for the end of a string by comparing a char against a null character is only valid for C-style strings and is not necessary for C++ standard library strings.
Upvotes: 0
Reputation: 846
Your function UserInput needs to take the string by reference. You pass it by value so the value does not get returned. Put a cout << password in PasswordMatch and you will see that it is empty.
The compare function returns 0 when the strings match. So you don't want the ! in the check. You could change it to be password != second_password if you want the not.
Upvotes: 0