I'm writting a small password program where the user has to enter a password and the password is check, the program should allow the user three attempts to enter the password if it does not meet the criteria, after the three attempts the program is suppose to terminate.
The problem I'm having is figuring out how to allow the user to enter the password three times.
Any suggestions or pointers in the right direction would be helpful, below is the code I've written so far.
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int const MAXNAME = 20;
int const MAX = 30;
bool compareName (char [], char []);
bool lengthOf ( char password []) ;
bool lowerAlpha (char password []);
bool upperCase (char password []);
bool lowerCase (char password []);
bool digitCheck (char password[]);
bool nonAlpha (char password[]);
bool nonSpace (char password[]);
bool checkAll (char username [], char password[]);
int main ()
{
char username [MAXNAME];
char password [MAX];
cout << " Please enter your user name: ";
cin.getline (username, MAXNAME);
checkAll ( username, password);
system ("Pause");
return 0;
}
bool checkAll (char username [], char password[])
{
cout << "\n Please enter your password: ";
cin.getline (password,MAX);
compareName (username,password);
lengthOf (password);
lowerAlpha(password);
upperCase (password);
lowerCase (password);
digitCheck(password);
nonAlpha (password);
nonSpace (password);
}
/*
Purpose: To compare cstring names
PreCondition: User enters name, and password
PostCondition: return a bool value
*/
bool compareName (char username [], char password[])
{
cout << " here " << endl;
if (strcmp (username,password) == 0)
{ // 0 in this case means true.
cout << " - Password and user name are the same" << endl;
return false;
}
}
/*
Purpose: To see if the password contains between 8-12 characters
PreCondition: User has entered password
PostCondition: return a bool value
*/
bool lengthOf ( char password [])
{
if ( strlen(password) <8 || strlen (password) > 12)
cout << " - Password must be between 8-12 characters long" << endl;
return false;
}
/*
Purpose: To see if password contains either an uppercase or a lowercase alphabetic letter
PreCondition: User has entered password
PostCondition: Return a bool value
*/
bool lowerAlpha (char password [])
{
bool compare1,
compare2;
for (int i = 0; i < strlen (password); i++)
{
if (isalpha (password [i]))
return true;
}
cout << " - Password must contain alphanumeric character" << endl;
return false;
}
/*
Purpose: To see if password is upper case
Post-Condition: User has entered password
Pre-Condition: return a bool value
*/
bool upperCase (char password [])
{
for (int i = 0; i < strlen (password); i++)
{
if (isupper (password [i]))
return true;
}
cout << " - Password must contain an upper case letter" << endl;
return false;
}
/*
Purpose: To see if password is lowercase !!!
pre-Condition: User has entered password
post-Condition: return a bool value
*/
bool lowerCase (char password [])
{
for (int i = 0; i < strlen (password); i++)
{
if (islower (password [i]))
return true;
}
cout << " - Password must contain an lower case letter" << endl;
return false;
}
/*
Purpose: To see if password has a digit
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool digitCheck (char password[])
{
int digit = 0;
for (int i = 0; i < strlen (password); i++)
{
if (isdigit (password[i]))
{
digit = atoi (password);
return true;
}
}
cout << " - Password must contain a number" << endl;
return false;
}
/*
Purpose: To see if password has nonalpha numeric character
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool nonAlpha (char password[])
{
for (int i = 0; i < strlen (password); i++)
{
if (!isalnum (password[i]))
{
return true;
}
}
cout << " - Password must contain a non-alphanumeric character" << endl;
return false;
}
/*
Purpose: To see if password has a white space
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool nonSpace (char password[])
{
for (int i = 0; i < strlen (password); i++)
{
if (isspace (password[i]))
{
cout << " - Password must contain no white space" << endl;
return false;
}
}
return true;
}

New Topic/Question
Reply



MultiQuote



|