【问题标题】:C++ arrays, expression must have a constant valueC++ 数组,表达式必须有一个常量值
【发布时间】:2021-01-14 13:26:28
【问题描述】:

好的,所以我是编码新手,正在尝试学习 c++。我正在制作一个程序来验证密码是否包含大写、小写和数字。我觉得解决方案将是一个简单的解决方案,但我终其一生都无法解决。

using namespace std;

    string password ="";
    cin >> password; 
  

我可以验证这一点。然后我想将密码转换为字符数组,以便检查密码的每个字符。我开始:

    char passwordHolder[password.length()];

但我得到了错误:

表达式必须有一个常数值

通过查看其他论坛的回复,我认为这与 Visual Studio 作为编译器无法处理变量数组有关,但我并不真正了解这种情况如何/为什么会发生,或者如何解决这个问题。

另一篇文章建议使用 new 运算符,但我不完全了解如何以有效的方式将其实现到我的代码中。

感谢任何帮助。

【问题讨论】:

  • char passwordHolder[password.length()]; 尝试分配一个非 const 值的字符数组。 C++ 不支持变长数组。请改用std::vector<char> passwordHolder(password.length());
  • 旁注:string 包含一个字符数组。你可能不需要一个新的。通过 stringpassword[index]、迭代器或基于范围的 for 循环来华尔兹。如果你真的想炫耀,algorithm library 中可能有一些东西可以为你完成大部分繁重的工作(提示:有)。
  • char passwordHolder[password.length()];variable-length array 而 C++ 没有这些。

标签: c++ arrays string function


【解决方案1】:

啊,我终于明白了。感谢 user4581301 告诉我一个字符串已经是一个字符数组。这个提示给了我如何解决问题的想法。

我实际上设法完全摆脱了新数组,而是搜索字符串。

我可以完全摆脱 char passwordHolder[password.length()]; 而不是它。

我最初的计划是搜索 passwordHolder 数组:

for (int i = 0; i < password.length(); i++){
  if (isupper(passwordHolder[i])){
    hasUpper= true;
  }
  if (islower(passwordHolder[i])){
    hasLower= true;
  }
  if (isdigit(passwordHolder[i])){
    hasDigit = true;
  }
}
if (hasLower == true && hasUpper == true && hasDigit == true)
        return 1;

但鉴于我不再需要 passwordHolder 数组,我可以改为使用 password 作为数组并执行以下操作:

for (int i = 0; i < password.length(); i++) {
        if (isupper(password[i]))
            hasUpper = true;
        else if (islower(password[i]))
            hasLower = true;
        else if (isdigit(password[i]))
            hasDigit = true;
    }
    if (hasLower == true && hasUpper == true && hasDigit == true)
        return 1;

感谢评论的人。我已经被困在这个问题上 3 个小时了,哈哈。

如果您对此任务也有疑问,我的完整解决方案在这里。可能仍然很邋遢,但它有效:

#include <iostream>
#include <string>
using namespace std;


string password = "";
string confirmPassword = "";

bool hasDigit = false;
bool hasUpper = false;
bool hasLower = false;

int x = 0;

int confirm(string password, bool hasUpper, bool hasLower, bool hasDigit)
{

    for (int i = 0; i < password.length(); i++) {

        if (isupper(password[i]))
            hasUpper = true;
        else if (islower(password[i]))
            hasLower = true;
        else if (isdigit(password[i]))
            hasDigit = true;
    }
    if (hasLower == true && hasUpper == true && hasDigit == true)
        return 1;
}

int main(string password, bool hasUpper, bool hasLower, bool hasDigit) {

Start:

    cout << "please enter your password: ";
    cin >> password;
    cout << "please confirm your password: ";
    cin >> confirmPassword;

    while (password != confirmPassword || password.length() < 8) {

        cout << "Passwords do not match. Please enter your password again: ";
        cin >> password;
        cout << "Please confirm your password: ";
        cin >> confirmPassword;
    }
    
    x = confirm(password, hasUpper, hasLower, hasDigit);

    if (x == 1) {
        cout << "You have a good password";
    }
    else {
        cout << "You should have a password with 8 characters or more, a Capital letter, lowercase letter, and a number. Try again. \n";
        goto Start;
    }

}

【讨论】:

  • 用循环替换 goto 语句
  • 缺少return语句会导致返回未定义的值。
  • 您使用的是 c++11 或更高版本? string 也有迭代器,因此您可以将 index-for-loop 替换为基于范围的循环 for( char symbol : password),并将 password[i] 替换为 symbol
  • @Phil1970 你的意思是我在哪里缺少退货声明?它似乎按目前的方式工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2020-03-07
  • 2014-10-21
  • 1970-01-01
相关资源
最近更新 更多