Cześć. Piszę pewien program(coś ala footbal manager) i mam mały problem. Dlaczego nie działa mi dodawanie klubów do poszczególnych lig i jak wyświetlać i usuwać kluby tylko z określonych lig? Zaznaczyłem te problematyczne fragmenty strzałkami.
class Team
{
public:
string nameOfTeam;
string stadionName;
int budget = 0;
vector <Player> players;
vector <SportFacility> sportsObjects;
};
class ListOfTeams
{
private:
vector<Team> teams;
Team team;
public:
void addTeamToTheLeague()
{
cout << "Club name: "; getline(cin >> ws, team.nameOfTeam);
cout << "Stadium name: "; getline(cin >> ws, team.stadionName);
cout << "Budget of the club: "; cin >> team.budget;
teams.push_back(team);
cout << "d";
}
void showTeam()
{
cout << "Club name: "; cout << team.nameOfTeam << endl;
cout << "Stadium name: "; cout << team.stadionName << endl;
cout << "Budget of the club: "; cout << team.budget << endl << endl;
}
void deleteTeamFromTheLeague()
{
int choice;
cout << "What club would you like to delete? (Give a number): "; cin >> choice;
if (static_cast<size_t>(choice - 1) >= teams.size() || choice - 1 < 0)
{
cout << "There is no team with that number :( you're going to the menu..." << endl << endl;
return;
}
teams.erase(teams.begin() + (choice - 2));
cout << "Succesfull removed team!" << endl;
}
};
class League
{
private:
string nameOfLeague;
friend class ListOfLeagues;
};
class ListOfLeagues
{
private:
vector<League>leagues;
vector<ListOfTeams> clubs;
League object;
ListOfTeams listOfClubs;
int numberOfTheLeague = 1;
int numberOfTheClub = 1;
public:
void addLeague()
{
cout << "League name: "; getline(cin >> ws, object.nameOfLeague);
leagues.push_back(object);
}
void showAllLeagues()
{
if (leagues.empty() == true)
{
cout << "The list of leagues is empty! You are going to menu..." << endl << endl;
return;
}
for (int i = 0; i < static_cast<int>(leagues.size()); i++)
{
cout <<numberOfTheLeague++<<". " << leagues[i].nameOfLeague << endl;
}
numberOfTheLeague = 1;
}
void removeLeague()
{
cout << "Give the number of the league that you want to remove" << endl;
int choice; cin >> choice;
//if(leagues.empty()== true || leagues.at(choice-1))
if (static_cast<size_t>(choice - 1) >= leagues.size() || choice - 1 < 0)
{
cout << "There is no league with that number :( you're going to the menu..." << endl << endl;
return;
}
leagues.erase(leagues.begin()+(choice - 2));
cout << "Succesfull removed league!" << endl;
}
void addClubsToTheLeague()
{
int choice;
cout << "Where would you like to put the team?(Give the number of the league) "; cin >> choice;
if (static_cast<size_t>(choice - 1) >= leagues.size() || choice - 1 < 0)
{
cout << "There is no league with that number :( you're going to the menu..." << endl;
return;
}
listOfClubs.addTeamToTheLeague();
//clubs.push_back(listOfClubs);
auto it = clubs.insert(clubs.begin() + (choice - 1), listOfClubs); //<- tu widzę problem, dlaczego nie dodaje zespołu do okreslonej ligi?
//clubs.insert(choice,listOfClubs);
}
void showLeagueTeams() // <- jak tu pokazywać zespoły tylko z określonej ligii?
{
if (clubs.empty() == true)
{
cout << "The list of this league teams is empty! You are going to menu..." << endl << endl;
return;
}
for (vector<ListOfTeams>::iterator it = clubs.begin();it !=clubs.end();it++)
{
cout << "Club number: " << numberOfTheClub++ << endl;
(*it).showTeam();
}
numberOfTheClub = 1;
}
void deleteLeagueTeam()
{
int choice;
cout << "What league would you like to put that team?(Give the number of the league): "; cin >> choice;
if (static_cast<size_t>(choice - 1) >= leagues.size() || choice - 1 < 0) //tu rzuce wyjątkami
{
cout << "There is no league with that number :( you're going to the menu..." << endl;
return;
}
listOfClubs.deleteTeamFromTheLeague();
clubs.erase(clubs.begin() + (choice - 1));
}
};
int main()
{
string name, surrname;
int choice;
ListOfLeagues list;
cout << "What's your name: "; cin >> name;
cout << "What's your surrname: "; cin >> surrname; cout << endl;
do
{
help(name, surrname);
cout << "Choice: "; cin >> choice;
switch (choice)
{
case 1:
list.addLeague();
break;
case 2:
list.showAllLeagues();
break;
case 3:
list.removeLeague();
break;
case 4:
list.addClubsToTheLeague();
break;
case 5:
list.showLeagueTeams();
break;
case 6:
list.deleteLeagueTeam();
break;
//...
default:
break;
}
} while (choice != 10);