[英]C++ arrays, expression must have a constant value
Ok, so I am new to coding and am trying to learn c++.好的,所以我是编码新手并且正在尝试学习 C++。 I am making a program to verify a password has uppercase, lowercase, and a number.我正在制作一个程序来验证密码有大写、小写和数字。 I feel that the solution will be a simple one but I cannot for the life of me work it out.我觉得解决方案将是一个简单的解决方案,但我终其一生都无法解决。
using namespace std;
string password ="";
cin >> password;
I can validate this fine.我可以验证这个罚款。 I then want to convert the password to an array of chars so I can check each character of the password.然后我想将密码转换为字符数组,以便我可以检查密码的每个字符。 I start with:我开始:
char passwordHolder[password.length()];
but I get the error:但我收到错误:
expression must have a constant value表达式必须有一个常量值
From looking at other forum responses I think it has something to do with visual studio as a compiler not being able to handle variable arrays, though I don't really understand how/why this happens, or how to work around this.从查看其他论坛的回复来看,我认为这与作为编译器无法处理变量数组的 Visual Studio 有关,尽管我真的不明白这是如何/为什么发生的,或者如何解决这个问题。
Another post suggests using the new
operator but I don't understand fully how to implement this into my code in a way that works.另一篇文章建议使用new
运算符,但我不完全理解如何以有效的方式将其实现到我的代码中。
Any help is appreciated.任何帮助表示赞赏。
Ah I finally got it.啊我终于明白了。 Thanks to user4581301 for telling me that a string already is an array of characters.感谢 user4581301 告诉我一个字符串已经是一个字符数组。 This hint gave me ideas of how to solve the problem.这个提示给了我如何解决问题的想法。
I actually managed to get rid of the new array entirely and instead search through the string.我实际上设法完全摆脱了新数组,而是搜索字符串。
instead of char passwordHolder[password.length()];
而不是char passwordHolder[password.length()];
I could get rid of that entirely.我可以完全摆脱它。
My initial plan was to search the passwordHolder
array with:我最初的计划是搜索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;
but seeing as I no longer need the passwordHolder
array, I could instead use password
as the array and do:但是看到我不再需要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;
Thank you to those who commented.感谢评论的人。 I had been stuck on this for like 3 hours lol.我已经坚持了大约 3 个小时,哈哈。
If you are also having issues with this task, my full solution is here.如果您在执行此任务时也遇到问题,我的完整解决方案在这里。 Probably still very scruffy but it works:可能仍然很邋遢,但它有效:
#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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.