Hi guys! I have to write a code that checks to see if the password entered meets certain criteria and if it does not the whole function is supposed to start over. The criteria are that it must be atleast 12 characters and must have one upercase letter, one lowercase letter, one number, and one special character. I already have some of the code down but I don't know where to go from here. Any help with this would be greatly appreciated. Thanks in advance guys!!

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "password checker\n";
    cout << "enter a password: ";

    string password = "";
    int i = 0;
    bool length = false;
    bool upper = false;
    bool lower = false;
    bool number = false;
    bool special = false;

    getline(cin, password);

    while (length != false && upper != false && lower != false && number != false && special != false )
    {

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

            if (password.length >= 12)
            {
                length = true;
            }
            if (password[i] >= 'a' && password[i] <= 'z')
            {
                lower = true;
            }
            if (password[i] >= 'A' && password[i] <= 'Z')
            {
                upper = true;
            }
            if (password[i] >= '0' && password[i] <= '9')
            {
                number = true;
            }
            if (password[i] == '!' || password[i] == '@' || password[i] == '#' || password[i] == '$' || password[i] == '%' || password[i] == '&' || password[i] == '^' || password[i] == '*')
            {
                special = true;
            }

        }

    }

    system("pause");
    return 0;
}

Dani AI

Generated

A few focused fixes and a compact pattern that makes the intent clear.

As pointed out, call password.length() (or better password.size()). Two additional issues to watch: the loop condition is inverted in the original — you want to repeat until the password is valid, not while every flag is already true — and you must reset the check flags before scanning a newly-entered password. Put the prompt/scan inside a loop that repeats until a validation function returns true. That keeps the flow simple and makes testing each requirement obvious.

Here is a minimal, easy-to-read C++ style you can adapt:

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

bool is_valid_password(const std::string& pw) {
    if (pw.size() < 12) return false;
    bool upper = false, lower = false, digit = false, special = false;
    const std::string specials = "!@#$%^&*";
    for (char c : pw) {
        unsigned char ch = static_cast<unsigned char>(c);
        if (std::isupper(ch)) upper = true;
        else if (std::islower(ch)) lower = true;
        else if (std::isdigit(ch)) digit = true;
        else if (specials.find(c) != std::string::npos) special = true;
    }
    return upper && lower && digit && special;
}

int main() {
    std::string password;
    while (true) {
        std::cout << "Enter a password: ";
        std::getline(std::cin, password);
        if (is_valid_password(password)) break;
        std::cout << "Must be >=12 chars and include upper, lower, digit and one of: !@#$%^&*\n";
    }
}

Notes: cast to unsigned char before std::isupper/islower/isdigit to avoid UB with negative char values. Choose your special-character set explicitly (or use std::ispunct if you want every punctuation, but be mindful of locale and Unicode). For real applications, avoid echoing passwords, use proper input-hiding, and rely on established libraries for strength checking and secure storage rather than rolling your own production authentication.

line 24 is incorrect -- password.length(); length is a function call so you need parentheses.

You might want to move the while loop on lines 21 and 22 up to be between lines 7 and 8 so that everything is inside that loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.