I'm having trouble with a program that creates a football league. The particular function I'm having trouble with is one which is supposed to display the team or teams with the best defense in the league ie.lowest goals against. I have the Get functions coded but the bestdefence is giving me trouble.
I can't seem to get it to just give me the team with the lowest goalsagainst. Instead it outputs each team with a lower value than is assigned to lowestgoalagainst
For example if I run it and lowestgoalagainst is set to equal a team with 3 goals against and the other team have 2 and 1 goals against respectively it will output both teams instead of just the lowest.
Sorry it's difficult to explain. Hopefully someone can give some assistance
You should just keep track of the lowest and then ouput that team's index?
1 2 3 4 5 6 7 8 9 10
int lowestGoalsAgainstIndex = 0, lowestGoalsAgainst = league[0].GetGoalsAgainst(); // set lowest to first team
for (int i = 0; i < numTeamsInLeague; i++)
{
if (league[i].GetGoalsAgainst() < lowestgoalsagainst) // check lowest against current indexed team
{
lowestGoalsAgainst = league[i].GetGoalsAgainst(); // update lowest goals
lowestGoalsAgainstIndex = i; // store index for "lowest" team
}
}
cout << league[lowestGoalsAgainstIndex].GetName() << " " << lowestGoalsAgainst; // output team name and goals against stat
EDIT: If there are multiple teams tied for lowest, obviously this method wouldn't do