Another program help: This program is suppose to check a password based on a cri
ID: 3541215 • Letter: A
Question
Another program help:
This program is suppose to check a password based on a criteria and accept or have you try again.
password should have 1 upper case, 1 lower case and be at least 6 characters.
When i run the code, i test with the password "lol" and it simply closes out. any suggestions?
// Program that asks and verifies password criteria
//headers
#include<iostream>
#include<string>
using namespace std;
void main()
{
string password;
int length;
bool Case = false,
digit = false;
//Enter password
cout<<"Please enter your password: ";
cin>>password;
//checking password to meet length and character type criteria
length=password.length();
if(length<6)
cout<<"Error, password must contain at least 6 Characters, please try again."<<endl;
for(int i=0;i<length;i++)
{
if(isupper(password[i])||islower(password[i]))
Case=true;
if(isdigit(password[i]))
digit=true;
}
if(!digit)
cout<<"Password needs to contain at least one number."<<endl;
if(!Case)
cout<<"Password needs to include at least one lower case and one upper case character."<<endl;
system("pasue");
}
//end of function
Explanation / Answer
It closes out because there is no do/while or while loop to repeat the process if triggers are not met. Also you want to split the check for upper and lowercase. You don't want to use OR in this case. If all the cases were lowercase it'll still trigger true even tho you want at least 1 upper and lower. I added what was needed if you don't understand a part of it just ask.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string password;
int length;
bool lCase = false,
uCase = false,
digit = false;
while(1)
{
//reset flags
lCase = false;
uCase = false;
digit = false;
//Enter password
cout<<"Please enter your password: ";
cin>>password;
//checking password to meet length and character type criteria
length=password.length();
if(length<6){
cout<<"Error, password must contain at least 6 Characters, please try again."<<endl;
continue; //brings it back to the front of the while loop
}
for(int i=0;i<length;i++)
{
if(isupper(password[i]))
lCase=true;
if(islower(password[i]))
uCase=true;
if(isdigit(password[i]))
digit=true;
}
if(!digit)
cout<<"Password needs to contain at least one number."<<endl;
if(!(lCase && uCase))
cout<<"Password needs to include at least one lower case and one upper case character."<<endl;
if(lCase && uCase && digit){
cout<<"Password accepted!" <<endl;
break;
}
}
system("pause");
return 0;
}
//end of function