I'm writing a program that stores passwords into a char array then checks for 1 upper, lower, number and allows @ - _ .

For some reason it won't read the special chars - unless I include this function, but when I do it only evaluates those in the function, and just ignores all non alpha/numeric characters.

bool valChar(char password[], const int length)
{
    bool ch = false;
    for(int i = 0; i < length; i++)
    {
        if(isdigit(password[i]) != 0 || isalpha(password[i]) != 0 || password[i] == '@'|| password[i] == '.'|| password[i] == '-'|| password[i] == '_')
        {
            ch = true;
            cout << endl << password[i] << " " << ch << endl;
        }
    }

    if(ch == false)
    {
        cout << "\n** INVALID ENTRY **\n";
        cout << "\nYour password can only contain the characters\n";
        cout << "A-Z, a-z, 0-9, &, _, ., and -\n";
    }

    return ch;

}

out put:
Please choose 1, 2, or 3.
1. Save a new password
2. Display all passwords
3. Exit

Choose 1, 2, or 3: 1

------ New Entry ------------------------

Please enter your new password: @jlk

  • Your password must be between 8 and 15 characters in length
    Your password was 4

  • Your password must conatin at least one
    uppercase charater.

  • Your password must conatin at least one
    number.

@ 1

j 1

l 1

k 1

Please enter your new password: jkl^jkl

j 1

k 1

l 1

j 1

k 1

l 1

Please enter your new password:

The out put lists the number and then the boolean outcome when evaluated as per the function up above.

As you can see the first entry the special char is evaluated and placed within the array, but the second it isn't? Can anyone shed some light on this?

This is how I'm reading the entries in:

/* ------ FUNCTION: NEW ENTRY ------------------------------------
----------------------------------------------------------------*/
void newEntry(char entries[], int index[])
{
    char buffer[256];
    char password[8];
    bool val[4];
    bool validation = true;

    for(int i = 0; i < 3; i++)
    {
        val[i] = true;
    }
   int length = 0;

    cout << "\n------ New Entry ------------------------\n";
    cout << "\nPlease enter your new password: ";
    cin.getline(buffer,256);


    val[0] = valLength(buffer, password, length);
    val[1] = valUpper(password, length);
    val[2] = valLower(password, length);
    val[3] = valNum(password, length);
    val[4] = valChar(password, length);


    for(int i = 0; i < 5; i++)
    {
        if(val[i] == false)
        {
            validation = false;
        }
    }

    while(validation == false)
    {

        validation = true;
        cout << "\nPlease enter your new password: ";
        cin.getline(buffer,256);

        length = 0;
        val[0] = valLength(buffer, password, length);
        val[1] = valUpper(password, length);
        val[2] = valLower(password, length);
        val[3] = valNum(password, length);
        val[4] = valChar(password, length);

       for(int i = 0; i < 5; i++)
        {
            if(val[i] == false)
            {
                validation = false;
            }
        }
    }





    cout << "Done";
    return;

Dumb mistake - it wasn't seeing them because of this:

 if(isdigit(password[i]) != 0 || isalpha(password[i]) != 0 || password[i] == '@'|| password[i] == '.'|| password[i] == '-'|| password[i] == '_')
{
ch = true;
cout << endl << password[i] << " " << ch << endl;
}
}

I fixed the problem by adding this (in case anyone is interested)

bool ch = false;
    for(int i = 0; i < length; i++)
    {
        if(isdigit(password[i]) != 0 || isalpha(password[i]) != 0 || password[i] == '@'|| password[i] == '.'|| password[i] == '-'|| password[i] == '_')
        {
            ch = true;
        }
        else
        {
            ch = false;
            break;
        }
    }
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.