I am trying to search an array of object, for example lastName, a private variable of the object, however, the array might have more than one same last name from different objects.. i need to output all the same last name onto the screen, how should i do that?

code below will return index of the first target found.... how do i finish searching the whole array and display them? thank you

int searchLN(Name *inName[], string targetLastName, int j)
		{
			int index=0;
			bool found = false;

			while( (!found)&& (index<j) )
			{
				if((*inName[index]).getLastName()== targetID)
				{
					found= true;
					
				}

				else
				{
					index++;
				}
			}

			if(found)
				return index;

			else
				return -1;
		}

why can't the calling function check to see if there are more last names with the same value?

int index = searchLN( <parameters here > );
while( inName[index].getlastname() == targetLastName)
{
    cout << inName[index].getlastname() << " ";
    index++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.