0

So I have this piece of code that has given me some headache. This is a function. names is an array that contains names. I ask user input to search and then I want to check if the name contains the string from search. I don't know what to do and didn't find anything that helped me (i'm probably just stupid).

bool contains(string*& names, string*& search){
for(unsigned int j = 0; j < names.length(); j++){
    if(names[j].find(search) != string::npos){
        return true;
    }
}
return false;

}

1 Answer 1

1

The problem is that you need to pass the function find an array, not names[j], so changing names[j] with names works just fine. Also you don't need to make the strings pointers since you don't change them. This code works just fine for me:

bool contains(string names, string search){
for(unsigned int j = 0; j < names.length(); j++){
    if(names.find(search) != string::npos){
        return true;
    }
}
return false;

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.