I need help with this problem I am working on that essentially asks the user to input a password with a few guidelines:
1: at least 8 characters
2: at least 2 digits
3: must consist of only letters and digits
This is what I have so far. The password length works, except it outputs "Password must have at least 8 characters" for ever when you enter less than 8 characters. How do I make it output only once? I am not sure how to do 2 and 3 either
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string inputPassword;
cout << "Enter a password with the following guidlines:\n\n";
cout << "\t*Password must have at least 8 characters\n";
cout << "\t*Password must consist of only letters and numbers\n";
cout << "\t*Password must be at least 2 characters\n\n";
getline(cin, inputPassword);
for (int letters = 8; letters > inputPassword.length();)
{
cout << "Passsword must have at least 8 characters";
}
/*int digit = 2;
while (isdigit(inputPassword[digit]) < 2)
{
cout << "Password must have at least 2 characters";
digit++;
}*/
return 0;
}
int main()
{
string inputPassword;
cout << "Enter a password with the following guidlines:\n\n";
cout << "\t*Password must have at least 8 characters\n";
cout << "\t*Password must consist of only letters and numbers\n";
cout << "\t*Password must be at least 2 characters\n\n";
getline(cin, inputPassword);
if(inputPassword.length() <= 8)
{
cout << "Passsword must have at least 8 characters";
}
elseif (isdigit(inputPassword.length) < 2)
{
cout << "Password must have at least 2 characters";
}
return 0;
}
@SakurasouBusters Yes you are correct. It can't be less than or equal to because that isolates 8 as well. Do you know how to implement the other 2 rules?
int numDigits = 0;
for(int i = 0; i < inputPassword.length(); i++)
{
if(isdigit(inputPassword[i])) numDigits++;
}
if(numDigits < 2)
{
cout << "Password must have at least 2 digits!" << endl;
}
int main()
{
string inputPassword;
cout << "Enter a password with the following guidlines:\n\n";
cout << "\t*Password must have at least 8 characters\n";
cout << "\t*Password must consist of only letters and numbers\n";
cout << "\t*Password must be at least 2 characters\n\n";
getline(cin, inputPassword);
if(inputPassword.length() < 8)
{
cout << "-Passsword must have at least 8 characters\n";
}
int numDigits = 0;
for (int i = 0; i < inputPassword.length(); i++)
{
if (isdigit(inputPassword[i])) numDigits++;
}
if (isalnum(inputPassword.length()[i]))
{
cout << "Password can only contain letters and digits\n";
}
if (numDigits < 2)
{
cout << "Password must have at least 2 digits!\n";
}
return 0;
}