Paul Johnecheck Paul Johnecheck - 1 month ago 11
C++ Question

Search function within my class not working correctly

I have a class named "Division" that has an array of vectors inside of it.

#include <vector>
#include "Player.h"
#include <string>
#include "role.h"
class Division
{
public:
std::string m_name;
std::vector<Player> m_division;
int search(int lower, int upper, Role role);
};


The class above has plenty of functions in it, but they shouldn't be relevant.
The "player" player class is also pretty simple,

#include <string>
#include "Role.h"
class Player
{
public:
std::string m_name;
std::string m_battletag;
std::string m_email;
int m_sr;
Role m_role;
};


Role is a simple enum, can be ROLE_SUPPORT, ROLE_DPS, ROLE_TANK, or ROLE_ANY.
This next function is the one I have a problem with. The SR of the players should be within 0-5000, and I wish to be able to search through the vector of players for a player between an upper bound and a lower bound, which is what the following code is supposed to do.

int Division::search(int lower, int upper, Role role)
{
for (int i = 0; i < m_division.size(); ++i)
{
if ((m_division[i].m_sr <= upper && m_division[i].m_sr >= upper) && m_division[i].m_role == role)
{
return i;
}
}
cout << "None found within that range and with that role\n";
return -1;
}


Unfortunately it doesnt. In fact, no matter what i do, it returns -1 and prints the error message i created. I know that there is a solution to what its looking for, so why wont it work?
if i call it like i do in main,

cout << NA.getName(NA.search(2100, 2200, ROLE_ANY));


(NA is a division) it always returns -1 even though I know for a fact that there is a player between 2100 and 2200 who has role_any, and its in position 3. Please help.

i uploaded the full program to github here
any help would be greatly appreciated.
Testing.csv is a bunch of randomly generated numbers and strings, it is correctly formatted for the program. "Scrub Cup work" is where main resides, and all classes are the parse function are in their respective .h and .cpp files

Answer

if ((m_division[i].m_sr <= upper && m_division[i].m_sr >= upper)

if (a <= b and a >= b) is always false unless a is exactly equal to b