简体   繁体   中英

C++ arrays, expression must have a constant value

Ok, so I am new to coding and am trying to learn 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.

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.

Any help is appreciated.

Ah I finally got it. Thanks to user4581301 for telling me that a string already is an array of characters. 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()]; I could get rid of that entirely.

My initial plan was to search the passwordHolder array with:

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:

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.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM