IF I have seven objects of playerType player, player1, player2, player3...etc and I want to search to see if there is a certain player in these objects, how would I do that. I know my code isnt complete and is quite off but if someone could take my blindfold off and point in the right direction, I would appreciate it.

bool playerType::searchPlayer(string searchFirst, string searchLast)
{
    int i;
    bool found = false;
        for (i = 0; i < 6; i++)
            if (name.getFirstName == searchFirst && name.getLastName == searchLast)
            {
                found = true;
                break;
            }
            
        if (found)
            cout << "Player is in database! ";
        else
            return false;
}

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

IF I have seven objects of playerType player, player1, player2, player3...etc and I want to search to see if there is a certain player in these objects, how would I do that. I know my code isnt complete and is quite off but if someone could take my blindfold off and point in the right direction, I would appreciate it.

bool playerType::searchPlayer(string searchFirst, string searchLast)
{
    int i;
    bool found = false;
        for (i = 0; i < 6; i++)
            if (name.getFirstName == searchFirst && name.getLastName == searchLast)
            {
                found = true;
                break;
            }
            
        if (found)
            cout << "Player is in database! ";
        else
            return false;
}

When you say search for object could you possibly narrow it down to search for string instead?

Well I would be searching for two different strings

Member Avatar for iamthwee

Your code looks half decent, although I've not tried or tested it.

The only thing I would correct is...

if (name.getFirstName == searchFirst && name.getLastName == searchLast)
if (name[i].getFirstName == searchFirst && name[i].getLastName == searchLast)

So you would be looping through an array of objects or possibly even a vector of objects?

I believe the function you posted cannot be a method in playerType because you can't loop through multiple instances of the of the players from a individual instance. You have to loop through the players and call the method on each one individually - which makes your method much simpler. Remove the loop and simply pass back the bool (don't output in the method -- do the output after it returns).

How do I search through an array of objects to see if a player name is in the array of objects? The code I have in main I know is incorrect cause I get "Player is in database!" whether or not the player is in the database and it obviously prints six times.

bool playerType::searchPlayer(string searchFirst, string searchLast)
{
    bool found = false;
            if (getFirstName() == searchFirst && getLastName() == searchLast)
            {
                found = true;
            }
            
        if (found = true)
            cout << "Player is in database! ";
        else
            return false;
}

Here is the code in main that I have in a switch statement to access the search function

int n;
        cout << "Enter the first and last name of the player to search for." << endl;
        cin >> fname >> lname;
        for (n = 0; n < 6; n++)
            player[n].searchPlayer(fname, lname);
      break;

>if (found = true)
Now do you see why it's always saying that it's in the database?

yeah but if the strings dont compare then the found = true; statement isn't executed and found should be false still

>yeah but if the strings dont compare
That's exactly it. = is not comparison, it is an assignment operator. If you want to compare datatypes, you must use the == operator, provided that the types have a handler to compare them, as is the case with std::string.

What joeprogrammer means is that this

if (found = true)

is an assignment statements whereas this

if(found == true)

is a comparison statement. Assignment statements always return true, so the if statement is always true, irrespective of the value of found before this statement. Also, because you're assigning true to found in the conditional there is no chance it could be false after the assignment.

Assignment statements always return true, so the if statement is always true, irrespective of the value of found before this statement.

Well, not exactly. See this snippet:

int main (void)
{
    int i ;

    if ( i = 0 )
    {
        cout << "I AM entering the IF block" ;
    }
    else
    {
        cout << "I AM NOT entering the IF block" ;
    }

    getchar () ;
    return 0;
}

The answer as we all know is "I AM NOT entering the IF block".

As I said, you do not want to display the message in searchPlayer . Since you basically have it, I'll give this one to you:

bool playerType::searchPlayer(string searchFirst, string searchLast)
{
    bool found = false;
    if ((getFirstName() == searchFirst) && (getLastName() == searchLast))
    {
        found = true;
    }
    return found;
}
int  n;
    cout << "Enter the ... ";
    cin >> fname >> lname;
    for (n = 0; n < 6; n++)
    {
        if (player[n].searchPlayer(fname, lname))
        {
            cout << "Player " << n << " is ";
            cout << fname << ' ' << lname << endl;
        }
    }
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.