I am writing a program for class for a password validator. I need the password to be at least 6 characters, but I am also limiting it to a max of 10 (not required but wanted to).
The program works great and my other validations work until I try to add a password with more than 10 characters. I can go through and reenter a new password, and I get the message that it is now a valid password. But then I get a the following error:
Run-Time Check Failure #2 - Stack around the variable 'password' was corrupted.
I can get around the error if I set the width, but I know this isn't the correct way to handle it. Also I would think that by setting the width that the user would not be able to enter anything other than that size.
Here is my code:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
using namespace std;
//Function prototypes
bool validatePassword(char *); //function to test pw requirements
bool validateUppercase(char *); //function to test for upper case value
bool validateLowercase(char *); //function to test for lowercase value
bool validateNumber(char *); //function to test for a number value
bool validateLength(char *); //function to test for password length
int main()
{
//Variabes
char password[11]; //set the maximum number of char for password include the end delimiter
int size; //size of the array
//Prompt user to enter a password based on criteria
cout << "Please enter a password that meets the following criteria: \n";
cout << "1. A minimum of 6 characters up to a max of 10. \n";
cout << "2. Contains atleast one uppercase and one lowercase character. \n";
cout << "3. Contains atleast one number. \n\n";
//cout << "****Please note if you have more the 10 characters \n";
//cout << " the program will only accept the first 10***\n";
cout << "\nPlease enter your password: ";
cin >> password;
//cin >> setw(10) >> password; //set this so I would not get a runtime error
size = strlen(password); //determines the length of the password
do
{
while (!validatePassword(password)) //if the functions returns a false value
{
cout << "\nYour password does not meet the requirements. ";
cout << "\nEnter a new password: ";
cin >> password;
size = strlen(password);
}
} while (size > 10);
if (validatePassword(password))
{
cout << "\nYour password " << password << " meets the requirements. \n"; //if the function returns a true value
}
system ("pause");
return 0;
}
//This function calls the other validation functions
bool validatePassword(char *pass)
{
int size = strlen(pass);
return (validateLength(pass) && validateUppercase(pass) && validateLowercase(pass) && validateNumber(pass) == true);
}
//This function validates the length of the password
bool validateLength (char *pass)
{
int size = strlen(pass);
if (size >= 6 && size < 10)
return true;
else
{
cout << "\n\nThe password you entered either contained to little or to many characters.";
cout << "\nA minimum of 6 characters to a maximum of 10 is required.";
return false;
}
}
//This function checks to see if the password contains an uppercase char
bool validateUppercase(char *pass)
{
int size = strlen(pass);
for (int count = 0; count < size; count++)
{
if (isupper(pass[count]))
return true;
}
cout << "\n\nThe password must contain at least one uppercase character. ";
return false;
}
//This function checks to see if the password contains an lowercase char
bool validateLowercase(char *pass)
{
int size = strlen(pass);
for (int count = 0; count < size; count++)
{
if (islower(pass[count]))
return true;
}
cout << "\n\nThe password must contain at least one lowercase character. ";
return false;
}
//This function checks to see if the password contains an number char
bool validateNumber(char *pass)
{
int size = strlen(pass);
for (int count = 0; count < size; count++)
{
if (isdigit(pass[count]))
return true;
}
cout << "\n\nThe password must contain at least one number. " ;
return false;
}
char password[11]tostd::string passwordand you're good to go. Thecharbuffer may be simply too short to hold your data.