C++ 为什么要进入无限循环?

C++ 为什么要进入无限循环?,c++,C++,我创建了一个程序,用户根据一些规范输入密码。整个程序已经完成,但我不明白的是,当密码满足要求时,为什么程序会进入无限循环。它会一直说“输入密码”和“密码有效”我尝试过在主函数中更改循环,也尝试过在isValid函数中更改循环,也尝试过放置break,但都是空手而归。关于为什么会发生这种情况的任何线索/提示?谢谢 #include <iostream> #include <cstring> using namespace std; void displayRequire

我创建了一个程序,用户根据一些规范输入密码。整个程序已经完成,但我不明白的是,当密码满足要求时,为什么程序会进入无限循环。它会一直说“输入密码”和“密码有效”我尝试过在主函数中更改循环,也尝试过在isValid函数中更改循环,也尝试过放置break,但都是空手而归。关于为什么会发生这种情况的任何线索/提示?谢谢

#include <iostream>
#include <cstring>

using namespace std;

void displayRequirements();
void displayResult (char[]);
bool hasUpper (char []);
bool hasLower (char []);
bool hasDigit (char []);
bool hasLength(char []);
bool isValid (char []);

int main ()

{
    displayRequirements();
    
    const int size = 7;
    char password [size];
    int length;
    
    length = strlen(password);
    
    while (length < size)
    {
        cout << endl;
        cout << "Enter a password: ";
        cin.getline(password, size);
        length = strlen(password);
        
        isValid (password);
    }
    
    cout << endl;
    
    return 0;
}

void displayRequirements()
{
    cout << "Password Requirements: " << endl;
    cout << "   - The password should be at least 6 characters long." << endl;
    cout << "   - The password should contain at least one uppercase." << endl;
    cout << "   and at least one lowercase letter." << endl;
    cout << "   - The password should have at least one digit." << endl;
}

bool hasUpper (char input[])
{
    for (int count = 0; count < strlen(input); count++)
    {
        if (isupper(input[count]))
        {
            return true;
        }
    }
    return false;
}

bool hasLower (char input[])
{
    for (int count = 0; count < strlen(input); count++)
    {
        if (islower(input[count]))
        {
            return true;
        }   
    }
    return false;
}


bool hasDigit (char input[])
{   
    for (int count = 0; count < strlen(input); count++)
    {
        if (isdigit(input[count]))
        {
            return true;
        }
    }
    return false;
}

bool hasLength(char input[])
{   
    int size = 6;
    int length = strlen(input);
    
    if (size == length)
    {
        return true;
    }
    return false;
}

void displayResult(char input[])
{
    if (!hasLength(input))
    {
        cout << "It should be at least 6 characters long." << endl;
    }
    
    if (!hasDigit(input))
    {
        cout << "It should have at least one digit." << endl;
    }
    
    if (!hasLower(input))
    {
        cout << "It should contain at least one lowercase letter." << endl;
    }
    
    if (!hasUpper(input))
    {
        cout << "It should contain at least one uppercase letter." << endl;
    }
}

bool isValid (char input[])
{
    int length = strlen(input);

    for (int count = 0; count < 1; count++)
    {
        if (input[length] != hasUpper(input) || hasLower(input) || hasDigit(input) || hasLength(input))
        {
            cout << endl;
            cout << "The password is invalid." << endl;
            cout << endl;
            displayResult(input);
        }
        
        else if (input[length] == hasUpper(input) || hasLower(input) || hasDigit(input) || hasLength(input))
        {
            cout << endl;
            cout << "Password is valid" << endl;\
            break;
        }
    }

    return 0;
}
#包括
#包括
使用名称空间std;
无效要求();
无效显示结果(字符[]);
bool-hasUpper(char[]);
bool-hasLower(char[]);
布尔数字(字符[]);
bool-hasLength(char[]);
bool是有效的(char[]);
int main()
{
显示要求();
常数int size=7;
字符密码[大小];
整数长度;
长度=strlen(密码);
while(长度<尺寸)
{

cout我修改了您的解决方案,因此它将提示用户输入密码,直到提供有效的输入。
isValid
函数返回
true
false
,当
true
时,它只会中断while循环

顺便说一下,在
isValid
函数中使用单个for循环检查需求

#include <iostream>
#include <cstring>

using namespace std;

void displayRequirements();
void displayResult (char[]);
bool hasUpper (char[]);
bool hasLower (char[]);
bool hasDigit (char[]);
bool hasLength(char[]);
bool isValid (char[]);

int main ()
{
    displayRequirements();
    const int size = 7;
    char password [size];

    // Repeat until valid password is provided
    while (true)
    {
        cout << endl;
        cout << "Enter a password: ";
        cin >> password;

        if(isValid(password))
            break;
    }
    return 0;
}

void displayRequirements()
{
    cout << "Password Requirements: " << endl;
    cout << "   - The password should be at least 6 characters long." << endl;
    cout << "   - The password should contain at least one uppercase." << endl;
    cout << "   and at least one lowercase letter." << endl;
    cout << "   - The password should have at least one digit." << endl;
}

void displayResult(bool hasUpper, bool hasLower, bool hasDigit, bool hasLength)
{
    if (!hasLength)
        cout << "It should be at least 6 characters long." << endl;

    if (!hasDigit)
        cout << "It should have at least one digit." << endl;

    if (!hasLower)
        cout << "It should contain at least one lowercase letter." << endl;

    if (!hasUpper)
        cout << "It should contain at least one uppercase letter." << endl;
}

bool isValid (char input[])
{
    int length = strlen(input);

    // flags
    bool hasUpper = false;
    bool hasDigit = false;
    bool hasLength = length >= 6;
    bool hasLower = false;

    for (int i = 0; i < length; i++){
        if (isdigit(input[i]))
            hasDigit = true;

        if (islower(input[i]))
            hasLower = true;

        if (isupper(input[i]))
            hasUpper = true;
    }

    // If all of the requirements are met
    if (hasUpper && hasDigit && hasLength && hasLower)
    {
        cout << endl;
        cout << "The password is valid." << endl;
        cout << endl;
        return true;
    }

    // If we got so far, it does not met one or more requirements
    cout << endl;
    cout << "Password is invalid" << endl;
    displayResult(hasUpper, hasLower, hasDigit, hasLength);
    return false;
}
#包括
#包括
使用名称空间std;
无效要求();
无效显示结果(字符[]);
bool-hasUpper(char[]);
bool-hasLower(char[]);
布尔数字(字符[]);
bool-hasLength(char[]);
bool是有效的(char[]);
int main()
{
显示要求();
常数int size=7;
字符密码[大小];
//重复此操作,直到提供有效密码
while(true)
{
密码;
如果(有效(密码))
打破
}
返回0;
}
无效要求()
{

cout
length=strlen(密码);while(长度
--您希望
length
等于什么?您还没有输入密码。在未初始化的
char
数组上调用
strlen
会调用未定义的行为,因为空字符可能在任何地方。的此描述表明
strlen(密码)
cin.getline(密码,大小)
之后将始终小于
size
,因为不同的函数处理nul终止符的方式不同。如果创建(MRE),您可能会自己看到问题所在。这至少会使自愿帮助您的人员更容易调试。实现MRE的第一步:接受所有密码(因为接受是无限循环发生时)。我不明白
for
循环的作用是什么
是有效的
。它只执行一次,实际上不使用
count
变量。我也不理解
if/else if
条件。什么是
输入[长度]!=hassupper(输入)
应该检查吗?我认为至少其中一些
|
应该是
&&
s,但我不知道有效密码对您到底意味着什么。但是无论发生什么情况,
isValid
返回
false
,然后您无论如何都不会使用
isValid
的返回值,而不是w一次编写所有函数,从一个需求开始(如最小长度)然后对代码进行调试,直到它工作为止,然后编写代码并测试下一个需求,直到它工作为止,依此类推。在编写时,您的代码有许多问题,并且需要更复杂和更长的时间。显然,我们不做家庭作业,否则,您将无法自学。非常低效的代码,因为对于len密码,它将计算大约20倍的长度gth 6.是的,我提到过,但这超出了这个问题的范围,lemme refactor it每次尝试只做两次,对我来说很好,