0

For an assignment I am doing in school, we are asked to create a string array with names and phone numbers. The goal is to search for either a full name or partial name and display all relevant phone numbers, i.e. searching Palmer and pull up all strings that have the last name Palmer in it. I'm stuck, I can get the code to compile. I can use the string.find() method fine without an array, but now that I am using an array it won't even compile.

This is my Function:

void nameSearch(string list[], const int size, string userIn)
{
    // Search for names and display them. 
    cout << "Here are the names that match your input: " << endl;

    int index = 0;

    while (index < size)
    {
        size_t pos = list[index].find(userIn); // Search for user input
        if (pos != std::string::npos) 
        {
            std::cout << list[index] << endl;
        }
        else
        {
            std::cout << " " << endl;
        }
        index++;
    }
    return;
}

And it is producing this error:

Error LNK2019 unresolved external symbol "void __cdecl nameSearch(class std::basic_string,class std::allocator > const * const,int,class std::basic_string,class std::allocator >)" (?nameSearch@@YAXQBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HV12@@Z) referenced in function _main ConsoleApplication11 C:\Users\xehon\source\repos\ConsoleApplication11\ConsoleApplication11\ConsoleApplication11.obj 1

It's saying there is an error on line one, but all that is there is a comment.

My question is does C++ not have a way to search an array of strings or is it just something I am not doing right?

EDIT:: New Code:

 // Pull up a phone number based on name entered

#include "stdafx.h"
#include<iostream>
#include <string>

const int Size = 10;

std::string nameSearch(const std::string[], int, std::string); // Function Prototype

int main()
{

    std::string input; // User Input

// List of pre programmed names and numbers
    std::string Str1 = "Hoshikawa Tanaka, 678-1223";
    std::string Str2 = "Joe Looney, 586-0097";
    std::string Str3 = "Geri Palmer, 223-8787";
    std::string Str4 = "Lynn Lopez, 887-1212";
    std::string Str5 = "Holly Gaddis";
    std::string Str6 = "Sam Wiggins";
    std::string Str7 = "Bob Kain";
    std::string Str8 = "Tim Haynes, 586-7676";
    std::string Str9 = "Warren Gaddis, 223-9037";
    std::string Str0 = "Ron Palmer, 486-2783";

    const std::string array[Size] = { Str1, Str2, Str3, Str4, Str5, Str6, Str7, Str8, Str9, Str0 };

    std::cout << "Enter the name you wish to search for: " << std::endl;
    std::cin >> input;

    nameSearch(array, Size, input);

    return 0;
}
std::string nameSearch(std::string list[], const int size, std::string userIn)
{
// Search for names and display them. 
    std::cout << "Here are the names that match your input: " << std::endl;

    int index = 0;

    while (index < size)
    {
        size_t pos = list[index].find(userIn); // Search for user input
        if (pos != std::string::npos)
        {
            std::cout << list[index] << std::endl;
        }
        else
        {
            std::cout << " " << std::endl;
        }
        index++;
    }
    return userIn;
}

Still producing error

user4581301
  • 33,082
  • 7
  • 33
  • 54
Memo
  • 1
  • 2
  • 1
    You iterate over every string in the array and search it just like what you're doing now... About the error you're having, please paste the code into the question itself instead of posting a crappy image. – eesiraed Apr 07 '18 at 18:01
  • Thanks, I was going to look up why some code has using namespace std today. So I fixed everything and updated the post, it is still pulling up the error. – Memo Apr 07 '18 at 18:30

2 Answers2

1

The forward declaration and the implementation

std::string nameSearch(const std::string[], int, std::string)
std::string nameSearch(std::string list[], const int size, std::string userIn)

are not the same. Making them more similar

std::string nameSearch(const std::string list[], int size, std::string userIn)
std::string nameSearch(std::string list[], const int size, std::string userIn)

shows the const moves to a different variable. End result, the nameSearch function provided does not match the nameSearch function promised by the forward declaration. They MUST be the same. size is passed by value and there's usually not much point to a copy being const, so give

std::string nameSearch(const std::string list[], int size, std::string userIn)

a try.

Side note: You may be able to make things a bit easier on yourself.

    std::string Str1 = "Hoshikawa Tanaka, 678-1223";
    std::string Str2 = "Joe Looney, 586-0097";
    std::string Str3 = "Geri Palmer, 223-8787";
    ...

    const std::string array[Size] = { Str1, Str2, Str3, ... };

can be reduced to

    const std::string array[Size] = { "Hoshikawa Tanaka, 678-1223", 
                                      "Joe Looney, 586-0097", 
                                      "Geri Palmer, 223-8787",
                                      ... };
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

Your function prototype differs from your function definition:

std::string nameSearch(const std::string[], int, std::string);

versus

std::string nameSearch(std::string list[], const int size, std::string userIn)

Top-level const qualifiers are not part of the function signature, but others do. You should declare your function as:

std::string nameSearch(std::string[], int, std::string);
YSC
  • 38,212
  • 9
  • 96
  • 149
  • Minor bug: `array` is `const`, so it can't be passed into a `std::string[]` parameter. You have to keep the `const`. – user4581301 Apr 07 '18 at 19:18