Compare strings
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;
}
SHWOO
Junior Poster in Training
73 posts since Jul 2006
Reputation Points: 41
Solved Threads: 0
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?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Well I would be searching for two different strings
SHWOO
Junior Poster in Training
73 posts since Jul 2006
Reputation Points: 41
Solved Threads: 0
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?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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).
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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;
SHWOO
Junior Poster in Training
73 posts since Jul 2006
Reputation Points: 41
Solved Threads: 0
>if (found = true)
Now do you see why it's always saying that it's in the database?
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
yeah but if the strings dont compare then the found = true; statement isn't executed and found should be false still
SHWOO
Junior Poster in Training
73 posts since Jul 2006
Reputation Points: 41
Solved Threads: 0
>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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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".
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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;
}
}
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944