Below is my code, but no matter what i type as a password, it says invalid passw
ID: 3635358 • Letter: B
Question
Below is my code, but no matter what i type as a password, it says invalid password, please enter again.The code needs 1 uppercase, 1 lowercase and 1 number and must have at least 6 characters.
The code may be written weird or "too much info" but I'm in computer science 1 and it has to be written in this form.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
// counting the number of characters: at least 6
int countNumChars(char pass[])
{
int num = 0;
int len = strlen(pass);
for (int count = 0; count < len; count++)
{
if (!isdigit(pass[count]))
{
return false;
}
else
{
num++;
}
}
return num;
}
// counting number of lower case characters: at least 1
int countLowerChars(char pass[])
{
int numa = 0;
int lena = strlen(pass);
for (int counta = 0; counta < lena; counta++)
{
if (!islower(pass[counta]))
{
return false;
}
else
{
numa++;
}
}
return numa;
}
// counting number of upper case characters: at least 1
int countUpperChars(char pass[])
{
int numb = 0;
int lenb = strlen(pass);
for (int countb = 0; countb < lenb; countb++)
{
if (!isupper(pass[countb]))
{
return false;
}
else
{
numb++;
}
}
return numb;
}
bool isValidPassword(char *password)
{
int len, numberChars, lowerCaseChars, upperCaseChars;
bool result = false;
len = strlen(password);
numberChars = countNumChars(password);
lowerCaseChars = countLowerChars(password);
upperCaseChars = countUpperChars(password);
result = ((len>=6)&&(lowerCaseChars>=1)&&(upperCaseChars>=1)&&(numberChars>=1));
return result;
}
int main()
{
char pass[100];
cout << "Please enter a password. ";
cout << "You must make sure your password has at ";
cout << "least one uppercase and at least one ";
cout << "lowercase letter and at least 1 number. ";
cin >> pass;
while (!isValidPassword(pass))
{
cout << "Invalid Password. Please type again. ";
cin >> pass;
}
cout << "Your password: " << pass << ", is good and accepted. ";
system("pause");
return 0;
}
Explanation / Answer
#include #include #include using namespace std; //function prototypes bool verifyPass(char []); bool hasUpperCase(const char * str); //returns true if str has at least 1 upper case bool hasLowerCase(const char * str); //returns true if str has at least 1 lowercase bool hasDigit(const char * str);//returns true if str has at least 1 digit bool isInvalidLength(int len); //returns true if len is invalid length const int LENGTH = 11; int main() { char password[LENGTH]; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.