for (i = 0; i < NUM_PLAYER; i++)
{
getData(player[i], i);
}
int choice;
while(choice != 6)
{
cout << "Enter 1 to input data, 2 to display original data, 3 to sort data \n";
cout << "4 to display sorted data, 5 to search by last name, 6 to exit the program: ";
cin >> choice;
cin.ignore();
cout << endl;
if (choice == 1)
{
for (i = 0; i < NUM_PLAYER; i++)
{
getData(player[i], i);
}
}
else if (choice == 2)
{
for (i = 0; i < NUM_PLAYER; i++)
{
showData(player[i], i);
}
}
else if (choice == 3)
{
for (i = 0; i < NUM_PLAYER; i++)
{
p2[i].lname = player[i].lname;
}
}
else if (choice == 5)
{
string look;
cout << "Enter part of last name to search: ";
cin >> look;
for (i = 0; i < NUM_PLAYER; i++)
{
int getNames = player[i].lname.find(look, 0);
if (getNames > 0)
{
cout << player[i].lname << endl;
}
}
}
cout << endl;
}
}
void getData(data &p, int j)
{
cout << "Last name for player " << (j + 1) << ": ";
getline(cin, p.lname);
cout << "First name for player " << (j + 1) << ": ";
getline(cin, p.fname);
for (i = 0; i < NUM_PLAYER; i++)
{
int getNames = player[i].lname.find(look, 0);
if (getNames > 0)
{
cout << player[i].lname << endl;
}
}
}
What's basically happening is the function find( ) is returning the position of the first match. If you enter "Smith" as a last name and then proceed to search "Smith" it finds the match at the 0th position, returning the value 0, which then fails to pass if(getNames > 0) even though the match was found.
To compare the two strings, you can always just shove them together in an if( ).