【问题标题】:Password Checking Program - Matching Password Failure - Looping Failure密码检查程序 - 匹配密码失败 - 循环失败
【发布时间】:2015-07-06 17:30:57
【问题描述】:

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

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

例子:如果我输入“密码”,它会输出输入错误的提示,但如果你再次输入正确,它将继续程序的其余部分。

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

}

【问题讨论】:

    标签: c++ c++11


    【解决方案1】:

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

    如果你想让你的输入函数修改密码值,你应该通过引用传递。

    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 和 null 字符来检查字符串的结尾只对 C 样式的字符串有效,对于 C++ 标准库字符串不是必需的。

    【讨论】:

    • 应用所有内容后,还有很多事情要做。又花了两个小时进行调试,然后我将长度检查放入测试循环,又经过一个半小时的调试,一切正常。谢谢你的帮助,非常感谢。
    【解决方案2】:

    您的函数 UserInput 需要通过引用获取字符串。您按值传递它,因此不会返回该值。在PasswordMatch中放一个cout

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-19
      • 2019-06-22
      • 2015-04-19
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      相关资源
      最近更新 更多