char lookup[LENGTH]; //Holding users input
char *strPtr = nullptr; //To hold point to the found product
int index; //Loop counter
//Prompt user for the persons name/phone number
cout << "Enter the partial name or phone number of the person you're looking for. This is case sensitive \n";
cin.getline(lookup, LENGTH);
//Search the array for the matching substring
for (index = 0; index < TELE_NUM; index++)
{
strPtr=strstr(contacts[index], lookup);
if (strPtr != nullptr) //Actual fix for names never showing "!="
break;
}
//If there was a matching name or phone number display the results
if (strPtr != nullptr)
{
cout << contacts[index] << endl;
foundNames = true;
}
else
if(!foundNames)
cout << "There was no person or phone number that matched your input" << endl;
Your 'search' stops on first match, does it not? Why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (index = 0; index < TELE_NUM; index++)
{
strPtr = strstr( contacts[index], lookup );
if (strPtr != nullptr)
{
cout << contacts[index] << endl;
foundNames = true;
}
}
if( !foundNames )
{
cout << "There was no person or phone number that matched your input" << endl;
}
PS. Why do you include <string>, but don't use std::string anywhere?
The program isn't stopping on the first thing it finds.
It's going thru the entire list, and any time it finds a match, it remembers a pointer to that match. The program is probably reporting the LAST thing it finds.
Instead of recording a pointer then printing the info out after the loop, why not do the input within the loop? That's the simplest answer to the problem - just output the answers as they are found. No pointer needed.
If you have to use pointers, then you're going to need to store a list of them in an array, or something like that.