Okay so the following program is supposed to decide whether or not a password is correct give that it is;
-No longer than 16 characters
-No shorter than 8 characters.
It contains at least:
-One uppercase letter
-One lowercase letter
-One number
and
-No special characters //(isalnum(name.at()))
My program will label all passwords as incorrect no matter what, it's breaking my C++ beginning heart </3 :(. Please #include<help> //Hashtag include help thanks.
-P.S. I know I don't need a lot returns true and false but it's just to get used to it for now.
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
bool isProper(string);
int main()
{
string password;
cout << "Enter a password that contains:"
<< "-No special characters.\n "
<< "-At least 1 upper, 1 lower case letter and 1 number \n"
<< "Between 8 and 16 characters long. \n";
cin >> password;
while(!isProper(password))
{
cout << "Enter a password: ";
cin >> password;
}
Your isProper function isn't doing what you think it's doing. I will say that it's returning prematurely. Can you figure out what's wrong by looking at the following example?
// program to test a valid password
// created by blongho, 2017-05-16
/*
Conditions
- No longer than 16 characters
- No shorter than 8 characters.
It contains at least :
- One uppercase letter
- One lowercase letter
- One number
and
-No special characters
*/
#include <iostream>
#include <string>
usingnamespace std;
// is using find_first_of()
//const string LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
//const string UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//const string NUMBERS = "0123456789";
bool isShorter(const string password); // check lower limit
bool isLonger(const string password); // check upper limit
bool hasUpperCase(const string password); // checks if it has uppercase
bool hasLowerCase(const string password); // checks if it has lowercase
bool hasNumber(const string password); // checks that password has a number
bool hasSpecialCharacters(const string password); // checks that it has no special characters
bool checkPassword(const string &password); // checks all conditions and prints result
void start(); // has all the magic
int main()
{
start();
return 0;
}
bool isShorter(const string password)
{
return password.size() < 8 ? true : false;
// same as
/*if (password.size() < 8) // password.size() is same as password.lenght();
{
return true;
}
return false;*/
}
bool isLonger(const string password)
{
return password.size() > 16 ? true : false;
}
bool hasUpperCase(const string password)
{
/*size_t found = password.find_first_of(UPPERCASE);
if (found != string::npos)
return true;*/
// check each character in password
for (auto &c : password)
{
if (isupper(c))
returntrue;
}
returnfalse;
}
bool hasLowerCase(const string password)
{
/*size_t found = password.find_first_of(LOWERCASE);
if (found != string::npos)
return true;*/
// check each character in password
for (auto &c : password)
{
if (islower(c))
returntrue;
}
returnfalse;
}
bool hasNumber(const string password)
{
/*size_t found = password.find_first_of(NUMBERS);
if (found != string::npos)
return true;*/
// check each character in password
for (auto &c : password)
{
if (isdigit(c))
returntrue;
}
returnfalse;
}
bool hasSpecialCharacters(const string password)
{
// check each character in password
for (auto &c : password)
{
if (!isalnum(c))
returntrue;
}
returnfalse;
}
// check validity of password
bool checkPassword(const string & password)
{
bool isGood = true;
if (!isShorter(password) && !isLonger(password) && hasUpperCase(password)
&& hasLowerCase(password) && hasNumber(password) && !hasSpecialCharacters(password))
{
cout << endl << "Proper password." << endl << endl;
isGood = true;
}
else
{
cout << endl << "Improper password! Reason..." << endl;
if (isShorter(password))
cout << "Your password is shorter than required (at least 8 characters)." << endl;
if (isLonger(password))
cout << "Your password can not be longer than 16 characters." << endl;
if (!hasUpperCase(password))
cout << "Your password must have at least one upper case letter." << endl;
if (!hasLowerCase(password))
cout << "Your password must have at least one lower case letter." << endl;
if (!hasNumber(password))
cout << "Your password must have at least one number." << endl;
if (hasSpecialCharacters(password))
cout << "No special characters permitted." << endl;
isGood = false;
}
return isGood;
}
// program starts and ends here
void start()
{
cout << endl << "Hello this program takes and validates your password" << endl << endl;
string password;
cout << endl << "Please enter your password: ";
getline(cin, password);
// continue asking for password as long as it does not satisfy condition
while (!checkPassword(password))
{
cout << endl << "Try another password: ";
getline(cin, password);
}
}
#include<iostream>
#include<cstdlib> // <--- Not sure if this is needed.
#include<string>
bool isProper(std::string);
int main()
{
std::string password{ "" };
bool good{ false };
// Changed the way this is displayed.
std::cout << "Enter a password that contains:"
<< "\n-No special characters.\n"
<< "-At least 1 upper case letter,\n1 lower case letter and \n1 number \n"
<< "Between 8 and 16 characters long. \n";
// Not needed because of change to while loop.
//std::cout << "\nEnter Password: ";
//std::cin >> password;
while (!good)
{
std::cout << "\n Enter a password: ";
std::cin >> password;
good = isProper(password);
}
if (good)
std::cout << "\n Your password is valid." << std::endl;
std::cout << "\n Your password is " << password << std::endl;
std::cin.get();
return 0;
}
bool isProper(std::string password)
{
bool digit{ false };
bool uppr{ false };
bool lowr{ false };
int numDigits{ 0 };
int numUppr{ 0 };
int numLowr(0);
if ((password.length() < 8) || (password.length() > 16))
returnfalse;
for (int i = 0; i < password.length(); i++)
{
if (isdigit(password.at(i)))
{
digit = true; // <--- Set not return.
numDigits++; // <--- May not be used or used later. Just an idea for now.
}
}
if (!digit)
std::cout << "\n Your password does not contain a number.\n";
// Other checks here.
return (digit && lowr && uppr);
}