• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Dodawanie do listy w liście

Object Storage Arubacloud
+1 głos
439 wizyt
pytanie zadane 4 marca 2021 w C i C++ przez kaminie318 Bywalec (2,070 p.)

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);

 

komentarz 4 marca 2021 przez VBService Ekspert (254,480 p.)

Może to będzie pomocne, szczególnie ostatni przykład.  smiley

4 unikalne sposoby na problemy ze zwracaniem wartości w C++

komentarz 4 marca 2021 przez kaminie318 Bywalec (2,070 p.)
Szczerze nie za bardzo rozumiem co ma dać mi ten optional. Nie mogę jakoś przerobić tego kodu i dodawać obiekty(kluby piłkarskie) do obiektów (ligi)? Np. żeby po indeksach tego ligowego wektora nadrzędnego dodawać te kluby .
komentarz 5 marca 2021 przez j23 Mędrzec (194,920 p.)

@kaminie318,

class ListOfLeagues
{
    League object;
    /* ... */

public:
    void addLeague()
    {
        cout << "League name: "; getline(cin >> ws, object.nameOfLeague);
        leagues.push_back(object);
    }
    /* ... */

Dlaczego object jest polem klasy, a nie zmienną lokalną metody addLeague? (listOfClubs też?)

Nie widzę sensu zaprzyjaźniania ListOfLeagues z League.

1 odpowiedź

+1 głos
odpowiedź 5 marca 2021 przez TOM_CPP Pasjonat (22,640 p.)

Zobacz jak można to uprościć na poniższym przykładzie:

#include <iostream>
#include <list>

using namespace std;

struct Team
{
    string teamName;
    string stadionName;
    int budget {0};
};

class ListOfTeams
{

private:
    list<Team> teams;

public:

    template< typename T >
    friend ListOfTeams& operator<<( ListOfTeams& listOfTeams , T&& team )
    {
        listOfTeams.teams.push_back(forward<T>(team));
        return listOfTeams;
    }

    friend ostream& operator<<( ostream& out , const ListOfTeams& listOfTeams )
    {
        for( const auto& team : listOfTeams.teams )
        {
            out << "Club name: " << team.teamName << endl;
            out << "Stadium name: " << team.stadionName << endl;
            out << "Budget of the club: " << team.budget << endl << endl;
        }
        return out;
    }

    bool remove( const string& name )
    {
        for( auto it {begin(teams)} ; it != end(teams) ; ++it )
        {
            if( it->teamName == name )
            {
                teams.erase(it);
                return true;
            }
        }
        return false;
    }
};


int main()
{
    ListOfTeams teamsList;
    cout << ( teamsList << Team{ "AcMonaco","StadionBlue",100000 } << Team{ "InterClub","StadionGreen",500000 } );

    Team team { "Real","StadionRed",700000 };
    teamsList.remove("InterClub");
    teamsList << team;
    cout << "After changes \n\n" << teamsList;
}

 

Podobne pytania

+1 głos
1 odpowiedź 2,071 wizyt
0 głosów
2 odpowiedzi 190 wizyt
pytanie zadane 27 września 2017 w HTML i CSS przez Bartlomiej Bywalec (2,480 p.)

92,634 zapytań

141,505 odpowiedzi

319,883 komentarzy

62,015 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...