I'm just starting C++ and creating a simple password validation program. [edit] My goal is to display each messages if there is a lack of uppercase, lower case, digits, and/or special characters.
If there is only special character the output will be like this:
Enter your password: !
Password must contain a lower case!
Password must contain an upper case!
Password must contain a digit!
Enter your password:
what i want if all the requirements are met:
Enter your password: 1qQ+
your password is created
program is finished
But what i got is infinite loop of "your password is created". Any solution or alternative to make my code better/efficient?
Sorry for bad intendation.
#include <iostream>
#include <string>
using namespace std;
void passcheck(string& password)
{
bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecialchar = false;
for (int i = 0; i < password.length(); ++i)
{
if (islower(password[i]))
{
hasLower = true;
}
else if (isupper(password[i]))
{
hasUpper = true;
}
else if (isdigit(password[i]))
{
hasDigit = true;
}
else if (password.find(" !#$%&'()*+,-.:;<=>?@[]^_`{|}~"))
{
hasSpecialchar = true;
}
}
do
{
if (!hasLower)
{
cout << "Password must contain a lower case!"<< endl;
}
if (!hasUpper)
{
cout << "Password must contain an upper case!"<< endl;
}
if (!hasDigit)
{
cout << "Password must contain a digit!"<< endl;
}
if(!hasSpecialchar)
{
cout << "Password must contain special char!"<< endl;
}
else
{
cout << "your password is created" << endl;
}
}while(hasSpecialchar && hasDigit && hasLower && hasUpper);
}
int main(int argc, char const *argv[])
{
string password;
do{
cout << "Enter your password: ";
getline(cin, password);
passcheck(password);
}while(true);
cout << "program is finished" << endl;
cin.get();
return 0;
}