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