Include a function named isValidPassword that will complete all the checks and return a Boolean value which will indicate whether the password is valid or not.
for this i am thinking i would remove my test in main and that will become my function definition but for the prototype would it return a int or a bool. I never got that good at functions and kind of get overwhelmed on where to draw the line or which way to follow.
second,
the additional requirement that the password contain at least one special character. assume that any character that is printable, not alphabetic, not numeric, and not a space character is "special". That includes punctuation, but may include additional non-punctuation characters. what would be the best way for something like this?
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
int main()
{
constint LENGTH = 101;
char List[LENGTH];
int Upper, Lower, Digit;
// Ask user to create a password
cout << "Create your password!\n"
<< "_____________________________________________\n"
<< "Passwords must meet the following criteria:\n" <<
"_____________________________________________\n"
<< "- The password must have at least six characters long.\n"
<< "- The password must have at least one uppercase letter.\n"
<< "- The password must have at least one lowercase letter.\n"
<< "- The password must have at least one digit.\n";
do
{
Upper = Lower = Digit = 0;
cout << endl << " Enter password: ";
cin.getline(List, LENGTH);
for (int i = 0; i < strlen(List); i++)
{
if (isupper(List[i]))
Upper++;
if (islower(List[i]))
Lower++;
if (isdigit(List[i]))
Digit++;
}
if (strlen(List) < 12)
cout << "Password is not at least twelve characters long.\n";
if (Upper == 0)
cout << "Password needs to have at least one uppercase letter.\n";
if (Lower == 0)
cout << "Password needs to have at least one lowercase letter.\n";
if (Digit == 0)
cout << "Password needs to have at least one digit.\n";
} while (Upper == 0 || Lower == 0 || Digit == 0 || strlen(List) < 6);
cout << " Password verified." << endl << " Your password is: " << List << endl;
return 0;
}
currently i got the second part down but am still struggling with the proper function use for isvalid.
This is what im trying but currently failing. I attempt what you suggest.
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
//
bool isValidPassword(string list);
int main()
{
if (password = true) {
cout << " Password verified." << endl << " Your password is: " << List << endl;
}
return 0;
}
bool isValidPassword(string password) {
constint LENGTH = 101;
char List[LENGTH];
int Upper, Lower, Digit, Special;
cout << "Create your password!\n"
<< "_____________________________________________\n"
<< "Passwords must meet the following criteria:\n" <<
"_____________________________________________\n"
<< "- The password must have at least Tweleve characters. ||\n"
<< "- The password must have at least one Uppercase letter. ||\n"
<< "- The password must have at least one Lowercase letter. ||\n"
<< "- The password must have at least one Digit. ||\n"
<< "- The password must have at least one Special character. ||\n";
do
{
Upper = Lower = Digit = Special = 0;
cout << endl << " Enter password: ";
cin.getline(List, LENGTH);
for (int i = 0; i < strlen(List); i++)
{
if (isupper(List[i]))
Upper++;
if (islower(List[i]))
Lower++;
if (isdigit(List[i]))
Digit++;
if (ispunct(List[i]))
Special++;
}
if (strlen(List) < 12)
cout << "Password needs to have at least twelve characters. ||\n";
if (Upper == 0)
cout << "Password needs to have at least one uppercase letter. ||\n";
if (Lower == 0)
cout << "Password needs to have at least one lowercase letter. ||\n";
if (Digit == 0)
cout << "Password needs to have at least one digit. ||\n";
if (Special == 0)
cout << "Password needs to have at least one special character. ||\n";
} while (Upper == 0 || Lower == 0 || Digit == 0 || Special == 0 || strlen(List) < 12);
}
the second part, make a lookup table of the ascii table if its ascii. mark the letters and digits and such and unprintables, anything else is fair game.
#include <iostream>
#include <string>
#include <ctype.h>
usingnamespace std;
bool isValidPassword(string password);
int main()
{
std::string password;
do {
// read password from user
}
while (!isValidPassword(password));
}
bool isValidPassword(string password) {
bool ok = true;
// Passwords must meet the following criteria: -- you need to implement them
// The password must have at least Tweleve characters.
if (ok) {
ok = password.size() >= 12;
}
// The password must have at least one Uppercase letter.
if (ok) {
ok = false;
for (size_t i = 0; !ok && i != password.size(); ++i)
ok = isupper(password[i]);
}
// The password must have at least one Lowercase letter.
// The password must have at least one Digit.
// The password must have at least one Special character.
return ok;
}