c++ - String Verifier 字符循环无法正常工作

标签 c++ string loops verification

我的密码验证程序无法运行。我的循环似乎只迭代一次,我只是将“它”作为输出以查看它是否在不断迭代,但事实并非如此。我不确定为什么, bool 值正在工作,但它只迭代一次,如果我的第一个字母是小写字母,那么它会说我需要一个大写字母和一个数字,反之亦然,如果我的第一个字符是数字或大写。这是一项家庭作业,但我有点迷路了。任何帮助将不胜感激。

#include<iostream>
#include<string>
#include<cctype>



using namespace std; 


int main()
{
    const int LENGTH = 20;
    char pass[LENGTH];



    cout << "Enter a password, that's at least 6 characters long, one uppercase, one lowercase letter ";
    cout << " and one digit." << endl;
    cin.getline(pass,LENGTH);



    bool isdig = true;
    bool isdown = true;
    bool isup = true;
    bool correct = false; 





    for(int index = 0; correct == false; index++)
    {
        cout << "it" << endl;

        if(isupper(pass[index]) == 0)
        {isup = false;}

        if(islower(pass[index]) == 0)
        {isdown = false;}

        if(isdigit(pass[index]) == 0)
        {isdig = false;}



        if(isdig == true && isup == true && isdown == true)
        {correct = true;}



        if(index = LENGTH - 1)
        {
            if(isdig == false)
            {cout << "Your password needs a digit." << endl;}

            if(isup == false)
            {cout << "Your password needs an uppercase letter." << endl;}

            if(isdown == false)
            {cout << "Your password needs a lowercase letter." << endl;}

            cout << "Re-enter another password. " << endl;
            cin.getline(pass,LENGTH);

            index = 0;
            isdown = true;
            isup = true;
            isdig = true;
        }

    }


    system("pause");
    return 0;

}

最佳答案

问题可能出在这一行:

if(index = LENGTH - 1)

此处您分配 LENGTH - 1 的值给index,所以您总是被要求重新输入您的密码作为该表达式始终为真。

关于c++ - String Verifier 字符循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864272/

相关文章:

c++ - 寻找相当于 unix 脚本命令的 Windows

java - 从 Java 对象类到 C++

string - 使变量引用其自己的字符串内容

java - 如何检查无效输入并循环直到输入有效?

C++ [警告] 从 'char' 转换为 'double' ...但我不是...?

c++ - 部分类特化中模板方法的特化

java - 判断字符串是否包含 a-z 字符

java - 如何以特定方式交换字符串中的两个数字?

c - 如何从数组列表的中间删除一个字符?

php - MySQL 连接而不是 "nested while loop"