当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Password verification program

问题描述:

I need to write a program that allows a user to choose a password. The password has to be a single string of characters that contains no white space.

  1. The password must be 6 characters long, must have one lowercase letter, must have one uppercase letter, and must have at least one digit.

  2. When ever the password fails, display error message regarding which one of the criteria isn't met. Be specific with which one to number 1. Then make them re-enter.

  3. Once user enters valid password make them verify their original chosen password. If the entry doesn't match make them start all over. If it matches state password if valid.

Sample Output:

> Enter your password: Boy1

Password needs to have 6 or more characters.

> Enter your password: women1

Password needs to contain at least one uppercase letter.

> Enter your password: boy

Password needs to have 6 or more characters.

Password needs to contain at least one uppercase letter.

Password needs to contain at least one digit.

> Enter your password: Mtndew1

Now re-enter your password for verification: Mtndew

Password does not match. Start over.

> Enter your password: Mtndew1

Now re-enter your password for verification: Mtndew1

You have now entered a valid password.

Here is what I have so far but don't know how put all this together and make the output function like above we I run it.

#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.

do{

cout << "Enter your password: ";

cin.getline(password,SIZE);

length = strlen(password);

cout<< "Please enter a password with at least 6 characters.\n";

cin.getline(password,SIZE);

length = strlen(password);

} while (length < 6);

//Call function.

testNum(password,SIZE);

return 0;

}

//Function Definition

void testNum(char pswd[],int size)

{

int count;

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";

}

}

网友答案:

I modify your code a lot

  1. you use const int for array size but it can't use for another function so i use enum PWSIZE

  2. I make loop in main. if all case are pass then break the loop

  3. and password array size is too small. In lencheck condition says more than 6charset but array size is 6. It'll make buffer overrun. I offer you change std::string

and first you get password first time and check case(like upper lower else) next is re enter check

It seems little dirty but this is just concept..

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

enum PWSIZE //Array Size
{
    PASSWORD_SIZE = 20
};

//Function Prototype
int testNum(char []);
int re_enter(char []);

int main()
{
    char password[PASSWORD_SIZE]; //To hold password
    int length;

    length = strlen(password);
    while(1)
    {
        //Get the password.
        do{
            cout<< "Please enter a password with at least 6 characters.\n";
            cout << "Enter your password: ";
            cin.getline(password, PASSWORD_SIZE);
            length = strlen(password);
        }while(length < 6);

        //Call function.
        if(testNum(password))
            continue;           //if return 1 pass below
        if(re_enter(password))
            continue;

        break;
    }
    return 0;
}

int testNum(char pswd[])
{
    int count;
    bool upper_flag = 0, lower_flag = 0, digit_flag = 0;
    for (count = 0; count<strlen(pswd); count++)    //don't need to Size use strlen
    {

        if (isupper(pswd[count]))
            upper_flag = 1;
        else if (islower(pswd[count]))
            lower_flag = 1;
        else if (isdigit(pswd[count]))
            digit_flag = 1;
    }
    if(!upper_flag)
    {
        cout << "The password does not contain an uppercase letter.\n";
    }

    if(!lower_flag)
    {
        cout << "The password does not contain a lowercase letter.\n";
    }
    if(!digit_flag)
    {
        cout << "The password does not contain a digit.\n";
    }
    if(upper_flag && lower_flag && digit_flag)
        return 0;   //if all pass
    else
        return 1;
}

int re_enter(char  passwd[])
{
    char compare_password[PASSWORD_SIZE] = {0,};
    cout << "Re Enter Your password" <<endl;
    cin.getline(compare_password, PASSWORD_SIZE);
    if(strcmp(passwd, compare_password))
    {
        cout << "Password Not Match" << endl;
        return 1;
    }
    return 0;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: