..for manipulation in the main part of the program.

Basically Im trying to create a Class function that asks the user to enter in 3 different teams and their nationalities and then redisplay them when the main calls that function.

struct squadre {
			string teamn;
			string nat;
			} clube [N_TEAMS];

....
public:
			Team(void); //Creates an empty Team
			~Team(void);
			clube storeTeamName();

Where N_TEAMS = 3

clube Team::storeTeamName()
{
	
	for (n=0; n<N_TEAMS; n++)
	{
		cout << "Enter Team Name : ";
		getline (cin,clube[n].teamn);
		cout << "Enter Team Nationality : ";
		getline (cin,clube[n].nat);
	}
        return clube;

and then the main...

Team disp; // defining the class Team
	
	disp.storeTeamName();
	cout << "\nYou have entered these teams:\n";
	for (n=0; n<N_TEAMS; n++)
	{
	cout << clube[n].teamn;
	cout << " (" << clube[n].nat << " )\n";
	}
	system("Pause");

I realise Im going to have to assign the disp.storeTeamName() to something so it can mainpulated in the main but Im having trouble working out how Id transfer the object clube of the structure back to the main program. If anyone has any ideas Id be grateful. Sorry If I havent explained myself well enough.

Ive ommitted non relevant elements of the entire program just for ease.

Recommended Answers

All 2 Replies

If I understand your question correctly, you want to return a reference to one of the structure instances inside your object:

#include <iostream>

struct block {
  int data;
};

class block_collection {
  static const int _size = 10;

  block _base[_size];
public:
  int get_size() const { return _size; }
  block& get_block ( int i ) { return _base[i]; }
};

int main()
{
  block_collection blocks;

  for ( int i = 0; i < blocks.get_size(); i++ )
    blocks.get_block ( i ).data = i;

  for ( int i = 0; i < blocks.get_size(); i++ )
    std::cout<< blocks.get_block ( i ).data <<'\n';
}
commented: Cheers again! +1

Thanks Narue, thats exactly what I meant and the push I needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.