I have an assignment to create a program that asks the user to create a password. The password must be at least 6 characters long, contain at least one uppercase and one lowercase letter, and contain at least one digit. I have written a program, but no matter what the password is, it does not recognize upper or lower case letters. For example, if I run my program and enter 2RxteRys. I will get the message your password is invalid. Password must contain an uppercase letter. I cannot figure out what I did or did not do in my validation of the password to keep getting this error. This is a C++ program and the focus of the asssignment is on characters, strings, and the string class. Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;
//Function prototype for validating the password
bool validate(char[], int);
int _tmain(int argc, _TCHAR* argv[])
{
const int SIZE = 25; //Array size
char password[SIZE]; //To hold the password
char choice; //To hold user's choice to restart
do
{
//Get the user's password
cout << "Please enter a password.\n";
cout << "Password must be six characters long.\n";
cout << "Password must include at least one uppercase and one lowercase letter.\n";
cout << "Password must include at least one number.\n";
cin.getline(password, SIZE);
//Validate the password
if (validate(password, SIZE))
cout << "Your password has been accepted.\n";
else
cout << "Your password is invalid. Please try again.\n";
cout << "Would you like to try again? Y or N?\n";
cin >> choice;
//Validate choice
while (toupper(choice) != 'Y' && toupper(choice) != 'N')
{
cout << "Please enter Y or N?\n";
cin >> choice;
}
}
while (toupper(choice) == 'Y');
return 0;
}
//Function Definition
bool validate(char password[], int size)
{
int count; //Loop counter
const int SIZE = 6;
int length;
length = strlen(password);
//Test for length.
if (length < SIZE)
{
cout << "Your password must be at least 6 characters long.\n";
return false;
}
//Test for uppercase
for (count = 0; count < SIZE; count++)
{
if (!isupper(password[count]))
{
cout << "Your password must have at least one uppercase letter.\n";
return false;
}
}
//Test for lowercase
for (count = 0; count < SIZE; count++)
{
if (!islower(password[count]))
{
cout << "Your password must have at least one lowercase letter.\n";
return false;
}
}
//Test for digit
for (count = 0; count < SIZE; count++)
{
if(!isalnum(password[count]))
{
cout << "Your password must have at least one number.\n";
return false;
}
}
return true;
}
I'd appreciate any help/hints I can get. Thanks.

New Topic/Question
Reply



MultiQuote






|