Q:

Searching a vector of object pointers

I have a Player class where each object of the type Class has a name, wins, losses, and draws.  Each object of the Player class is created by calling the_player = new Player(the_name).  When the user inputs a new name to add a Player object to the program a pointer is pushed into a vector AllPlayers.  The program should check before pushing the new pointer that the desired player does not already exist in said vector.  I have to do this check several times throughout my program so I thought I'd write a function for it.  Here is my function:
int findPlayer(vector<Player*> &vals, string name_in)
{
    for (int i = 0; i < vals.size(); i++){
        if (vals[i]->getName() == name_in){
            cout << vals[i]->toString() << endl;
            return i;
        }
        else
            return -1;
    }
};

When the option is requested to add a new Player the following code is used:
do {
    cout << "Name: "; 
    cin >> the_name;

    if (findPlayer(AllPlayers, the_name) != -1){
        cerr << "Player already exists\n";
    }
} while (findPlayer(AllPlayers, the_name) != -1);

the_player = new Player(the_name);
AllPlayers.push_back(the_player);

For some reason, though, every time I try to add a new player it throws "Player already exists" and never leaves the do-while loop.  This is even true when the AllPlayers vector is empty.  I added a cout << findPlayer(AllPlayers, the_name) for debugging and it printed 4192252 which I assume is the largest element possible in a vector.
So the question is: Why does it return 4192252 rather than -1?

A:

If vals is empty then the for loop is never entered and the function exits without hitting a return statement. Meaning that you get a random value returned instead, in this case 4192252 happens to be in the return register. Your compiler warnings will have told you this if you read them.

A:

What you think, what will be returned from findPlayer if vals is empty?
Is it defined?

A:

The find player function should be something like:
int findPlayer(vector<Player*> &vals, string name_in)
{
    if(vals.size() == 0)
        return -1;

    for (int i = 0; i < vals.size(); i++){
        if (vals[i]->getName() == name_in){
            cout << vals[i]->toString() << endl;
            return i;
        }
    }
    return -1;
};

