密码检查程序 - 匹配密码失败 - 循环失败

时间:2015-04-28 03:20:13

标签: c++ c++11

正如标题所述,该程序是供用户输入密码的。该程序检查密码传递的四个规则(1)。然后它会提示用户再次输入密码进行验证,程序然后将其与初始密码(2)进行比较,并说明两个密码是否匹配且是否可接受。

(1) - 第一个规则,检查字符串长度(必须至少8个字符长),循环似乎表明它失败,无论它是否成功。也就是说,如果DID实际上失败了,程序会让用户输入密码,直到用户输入通过此规则的密码。

示例:如果我输入“password”,它将输出错误输入的提示,但如果再次正确输入,将继续执行其余程序。

(2)我似乎无法正确比较两个字符串以检查它们的准确性,并尝试了几种技术来确定它是否会改变。许多方法导致while循环完全取消。

以下是代码:(感谢我所有的帮助,现在已经开始使用这个程序很长时间了,无济于事)

/*This program determines if the entered password is valid and if the second password entry matches the first.
*/

#include <iostream>

#include <cstring>

#include <string.h>

#include <string>

using namespace std;

void UserInput(string password);
short PasswordAlphaNumCheck(string password);
void PasswordValidationOutput(string password);
void PasswordMatch(string password, string second_password);

int main()
{
    string password;
    string second_password;

    UserInput(password);
    PasswordValidationOutput(password);
    PasswordMatch(password, second_password);

    return 0;
}

void UserInput(string password)
{
    cout << "The Password that you are entering must: " << endl;
    cout << " " << endl;
    cout << "1) Be at least 8 characters long" << endl;
    cout << "2) Contain at least one number" << endl;
    cout << "3) Contain at least one upper case letter" << endl;
    cout << "4) Contain at least one lower case letter" << endl;
    cout << " " << endl;

    cout << "Please enter a password now: " << endl;
    cin >> password;

}

short PasswordAlphaNumCheck(string password)
{
    short flag[3] = {0};

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

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

}

void PasswordValidationOutput(string password)
{
    short test = PasswordAlphaNumCheck(password);

    while ((password.length() < 8) && (test < 7))
    {
        while (password.length() < 8)
        {
            cout << " " << endl;
            cout << "Your password is not the correct size." << endl;
            cout << "Please re-enter your password." << endl;
            cin >> password;

        }

        cout << " " << endl;

        test = PasswordAlphaNumCheck(password);

        cout << password
        << " is "
        << (test == 0 ? "not supposed to cause this error. Report to inept computer engineer." :
            test == 1 ? "not valid because you are missing an upper and lower case letter." :
            test == 2 ? "not valid because you are missing an lower case letter and a number." :
            test == 3 ? "not valid because you are missing a lower case letter." :
            test == 4 ? "not valid because you are missing an upper case letter and a number" :
            test == 5 ? "not valid because you are missing an upper case letter." :
            test == 6 ? "not valid because you are missing a number." : "valid") << endl;

        while (test < 7)
        {
            cout << " " << endl;
            cout << "Please re-enter your password." << endl;
            cin >> password;
        }
    }

    cout << " " << endl;
}

void PasswordMatch(string password, string second_password)
{
    cout << "Please enter your password again, for verification" << endl;
    cin >> second_password;

    while (!password.compare(second_password))
    {
        cout << "Your passwords do not match." << endl;
        cout << " " << endl;
        cout << "Please re-enter your password." << endl;
        cin >> second_password;

    }

    cout << "Your password has been approved!" << endl;

}

2 个答案:

答案 0 :(得分:0)

您的函数UserInput需要通过引用获取字符串。您按值传递它,因此不会返回该值。放一个cout&lt;&lt;密码在PasswordMatch中,你会看到它是空的。

当字符串匹配时,compare函数返回0。所以你不想要!在检查中。您可以将其更改为密码!= second_password如果您不想要。

答案 1 :(得分:0)

您的用户输入函数按值传递密码参数,因此不会更改函数外部的密码变量。因此,当您将密码传递到验证函数时,它仍然具有默认的空值。

如果您希望输入功能修改密码值,则应通过引用传递。

void UserInput(string& password);

您的验证功能还旨在更改密码变量的值,因此它也应该通过引用获取其参数。

void PasswordValidationOutput(string& password);

您的second_password变量在PasswordMatch函数之外没有任何用途,因此我建议您将该函数的签名更改为:

bool PasswordMatch(string password);

在此功能中,您可以在阅读之前声明string second_password

最后一件事。在PasswordAlphaNumCheck内的第一个循环中,您应该将password[i] != '\0'更改为i != password.length()。通过将char与空字符进行比较来检查字符串的结尾仅对C样式字符串有效,对于C ++标准库字符串不是必需的。