TPC4589
TPC4589

Reputation: 13

Need help passing arrays elements/info to functions C++

I am learning C++ and I am having some trouble using/writing functions.

right now I am working on a problem trying to validate a user provided password.

The requirements are:

1 upper case letter 1 lower case letter 1 number at least 6 characters

each requirment is meant to be validate by passing from main() to a function.

I wrote a function for the lower case letter and I keep getting error messages saying there is an unresolved external error. However, when I pass the user_password from the main to the length function it returns the proper statement.

This is the error I keep getting in Visual Studio:

Error 3 error LNK2019: unresolved external symbol "bool __cdecl lowercase(char * const)" (?lowercase@@YA_NQAD@Z) referenced in function _main

Error 4 error LNK1120: 1 unresolved externals

Here is the code I have written so far

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

using namespace std;

bool length(char[]);
bool lowercase(char[]);


int main()
{
    const int SIZE = 20;
    char user_password[SIZE];
    cout << "Please enter a password" << endl;
    cout << "**********************************************" << endl;
    cout << "Must contain at least 1 upper case letter" << endl;
    cout << "Must contain at least 1 lower case letter" << endl;
    cout << "Must contain at least 1 number" << endl;
    cout << "**********************************************" << endl;
    cin.getline(user_password, SIZE);

    length(user_password);

    lowercase(user_password);

    return 0;
}
bool length(char password1[])
{

    int length;
    length = strlen(password1);

    if (length < 6)
    {
        cout << "Password needs at least 6 characters" << endl;
    }
    else if (length < 6)
    {
        cout << "Character requirements met" << endl;
    }

    return password1;
}
bool lowercase(char password2)
{
    if (!islower(password2))
    {
        cout << "Password requires at least 1 lower letter" << endl;
    }
    else if (islower(password2))
    {
        cout << "Lower case letter requirement met" << endl;
    }

    return password2;
}

Upvotes: 1

Views: 70

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20063

This is because the signature on the function definition doesn't match the signature on the declaration.

bool lowercase(char[]);

bool lowercase(char password2)  // OOPS! Doesn't match.
{
    ....
}

should be

bool lowercase(char password2[])
{
    ....
}

You should really use either std::string or at the very least pass by const char pointer. Passing by const pointer is more identifiable with passing a pointer to a null terminated string.

bool lowercase(const char* password2)

Either way you will also need to change how you are calling islower since it accepts a character value instead of a pointer to a string. To to this you will need to iterate over the string in for or while loop and check each character individually. You're trying to return a pointer as a boolean value, this won't work. Something like this will:

bool lowercase(char password2[])
{

    for (const char*ptr = password2; *ptr; ++ptr)
    {
        if (!islower(*ptr))
        {
            cout << "Password requires at least 1 lower letter" << endl;
            return false;
        }
    }

    cout << "Lower case letter requirement met" << endl;
    return true;
}

As noted in the comments your implementation of length is also bonkers. not only are you checking for the same length twice but you're trying to return a pointer as a boolean value.

bool lowercase(char password2[])
{
    unsigned int notlower = 0;
    for (const char*ptr = password2; *ptr; ++ptr)
    {
        if (!islower(*ptr))
        {
            notlower++;
        }
    }

    if (notlower == strlen(password2))
    {
        cout << "Password requires at least 1 lower letter" << endl;
        return false;
    }

    cout << "Lower case letter requirement met" << endl;
    return true;
}

Upvotes: 2

Related Questions