Help with C++ password program

Microsoft Visual studio 2008 professiona...
November 17, 2009 at 08:27:55
Specs: Windows Vista
I need help writing a program for my C++ class. The assignment is to write a prgram where the user enters a password which must contain at least 6 characters (containing at least 1 lowercase, 1 uppercase, and one digit)and I need to validate the entry as indicated and after reading all of the characters, specify why the password is not acceptable. I have it working but it is reading each character and then outputs after each character the reason it didn't meet the requirements. I want the program to read all characters and then output only once explaining the exact reason it did not meet. Anything you can do to help me will be greatly appreciated!!!

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

//Function Prototype
void testNum(char [], int);

int main()
{
const int SIZE = 7; //Array Size
char password[SIZE]; //To hold password
int length;
length = strlen(password);

//Get the password.
cout<< "Enter a password.\n";
cin.getline(password, SIZE);
length = strlen(password);

while (length <6)
{
cout<< "Please enter a password with at least 6 characters.\n";
cin.getline(password, SIZE);
length = strlen(password);
}


//Call function.
testNum(password, SIZE);


return 0;
}
//Function Definition

void testNum(char pswd[], int size)
{

int count;

//Test for validity
for (count = 0; count<size-1; count++)
{
if (!isupper(pswd[count]))
cout << "The password does not contain an uppercase letter.\n";

if (!islower(pswd[count]))
cout << "The password does not contain a lowercase letter.\n";

if (!isdigit(pswd[count]))
cout << "The password does not contain a digit.\n";

}


}



See More: Help with C++ password program

Report •


#1
November 17, 2009 at 13:00:33
You should initialize your vars before you use them. For example, the password could contain junk before its ever used.

char password[SIZE] = { 0 };

You can use a do-while loop to make sure your password is the right size. That way, you can eliminate a few bits of code. With a slight modification, the same loop could be used to validate the input as well.

do {
   cout << "enter password";
   cin.getline(password, sizeof password);
} while(strlen(password) != 6);

For your check, there's probably multiple ways it can be done; perhaps something like below would work.

short isValid(const char *pw) {

   short flag[3] = { 0 };

   for(int i=0; pw[i] != '\0'; i++) {
      if(isdigit(pw[i])) {
         flag[0] = 1;
      } else if(isupper(pw[i])) {
         flag[1] = 2;
      } else if(islower(pw[i])) {
         flag[2] = 4;
      }
   }

   return (flag[0] + flag[1] + flag[2]);
}

To validate the password, perhaps something like below could be used.

short test = isValid(password);   

      cout << password 
           << " is" 
           << (test == 0 ? " invalid" :
               test == 1 ? " invalid - isdigit" :
               test == 2 ? " invalid - isupper" :
               test == 3 ? " invalid - isdigit / isupper" :
               test == 4 ? " invalid - islower" :
               test == 5 ? " invalid - isdigit / islower" :
               test == 6 ? " invalid - isupper / islower" :
               " valid")   << endl;
   }

Anyway, hope that helps a bit. Good luck.


Report •
Related Solutions


Ask Question