Okie dokie I have to write a program that takes a password and makes sure it has 8 characters, 1 capital, and 1 lowercase letter.
Now I know I'm close. I can get two or three different passwords to work and than some just don't. Any help is appreciated! Please explain with suggestions, my professor is not that great. Thank you!
#include <iostream>
#include <cctype>
#include <string>
usingnamespace std;
int main()
{
constint length = 8;
int password[length];
cout << "Please enter a password: " << endl;
cin >> password[length];
{
int i = 0;
int big = 0; // counter for big
int lil = 0; // counter for little
bool valid = false;
while (i > password[length])
{
if (password[length] >= 'a' && password[length] <= 'z')
{
lil++;
}
else
{
if (password[length] >= 'A' && password[length] <= 'Z')
{
}
else
{
big++;
}
}
i++;
}
if (8 >= password[length])
{
}
else
{
cout << "You need to have at least 8 characters in your password." << endl;
}
if (big == password[length])
{
cout << "You need at least one Uppercase letter." << endl;
}
else
{
if (lil == password[length])
{
cout << "You need to have at least one Lowercase letter." << endl;
}
else
{
}
}
if (valid = password[length])
{
}
else
{
cout << "Thank you, that is a valid password!" << endl;
}
}
system("pause");
return 0;
}
int main()
{
constint PASSWORD_MAX = 1024;
char password[PASSWORD_MAX];
cout << "Please enter a password: " << endl;
std::cin.getline(password, sizeof(password));
{
int i = 0;
int big = 0; // counter for big
int lil = 0; // counter for little
bool valid = true;
for (int i = 0; i < strnlen(password, sizeof(password)); i++)
{
if (password[i] >= 'a' && password[i] <= 'z')
{
lil++;
}
elseif (password[i] >= 'A' && password[i] <= 'Z')
{
big++;
}
}
if (8 > strnlen(password, sizeof(password)))
{
cout << "You need to have at least 8 characters in your password." << endl;
valid = false;
}
if (big == 0)
{
cout << "You need at least one Uppercase letter." << endl;
valid = false;
}
if (lil == 0)
{
cout << "You need to have at least one Lowercase letter." << endl;
valid = false;
}
if (valid)
{
cout << "Thank you, that is a valid password!" << endl;
}
else
{
cout << "invalid password!" << endl;
}
}
system("pause");
return 0;
}
bool check_password(const std::string password)
{
// make sure it has at lease 8 characters
if (password.size() < 8)
{
std::cout << "you need to have at lease 8 charaters" << std::endl;
returnfalse;
}
int numOfCapital = 0;
int numOfLowerCase = 0;
for (char c : password)
{
if (c >= 'a' && c <= 'z')
{
numOfLowerCase++;
}
if (c >= 'A' && c <= 'Z')
{
numOfCapital++;
}
}
// make sure it has at lease one capital
if (numOfCapital < 1)
{
std::cout << "you need to have at lease one capital letter" << std::endl;
returnfalse;
}
// make sure it has at least one lowercase
if (numOfLowerCase < 1)
{
std::cout << "you need to have at least one lower case letter" << std::endl;
returnfalse;
}
returntrue;
}
int main()
{
char password[256]; //8 characters, plus '\0' for teminating
std::cin.getline(password, sizeof(password));
bool valid = check_password(std::string(password));
if (valid)
{
std::cout << "password is valid" << std::endl;
}
else
{
std::cout << "password is invalid!" << std::endl;
}
system("pause");
return 0;
}