Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays of structures

Status
Not open for further replies.

ah01

Programmer
Joined
Jan 29, 2005
Messages
4
Location
US
#include <iostream>
using namespace std;

struct person
{
char name[15];
int *scores;
};


//// function prototype //////////
void allocate_arrays(int number_players,int number_scores);
//////////////////////////////////

int main()
{
int number_scores;
int number_players;

cout << "How many players in your team?: ";
cin >> number_players;
cout << endl;

cout << "How many scores each player will have?: ";
cin >> number_scores;
cout << endl;

/* pass the number of players and how many scores to memry allocation function: */

allocate_arrays (number_players , number_scores);

return 0;

} // end of main

///////////// functions defintions //////////////
void allocate_arrays(int number_players , int number_scores)
{
person *player;
player = new person[number_players];

int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];

/* the above statement gives error "can not convert from int* to int " using the last statement above instead of scores = new int [number_scores] */

return ;
} // end of allocate arrays function


to get the total scores for each player is easy by using a for-loops.
my questions are:

1)how to allocate memory for x number of scores for each player? in this format :

int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];
//for each player, he has number of scores array.

2) how to pass the scores pointer from main to the function? do I have to declare the pointer in the function.

3) scores pointer declared in the structure was it global to all functions?
 
You have at least 2 (different) problems:
1. Access to the current game info in functions.
2. Too low level structure game info.
Use CODE tag in your posts, please...
Try this (it's only a sample C++ solution;):
Code:
/// Declarations (make .h file)
struct Person // 2nd problem...
{
	explicit Person(int ns):scores(ns,0) {}
	string name;
	vector<int> scores;
	string toString() const;
};

class Game // see static last() - (one of) the 1st problem
           // solution...
{
public:
	explicit Game(int np = 0, int ns = 0):player(np,Person(ns)) { plast = this; }
	~Game();
	void resize(int np = 0, int ns = 0);
	Person& operator[](int i) { return player[i]; }
	/// get i-th player.
	const Person& operator[](int i) const 
        { return player[i]; }
	/// Get number of players.
	int	size() const { return player.size(); }
	/// Get default (last) game reference.
	static Game& last() { return *plast; }
	/// No games.
	static bool none();
	/// Sugar...
	static bool defined() { return !none(); }
	/// Add player on the fly.
	int addPlayer(Person& newp);
	/// Returns old game pointer (0 if no game).
	static Game* setDefault(Game& game);
	static Game* setDefault(Game* pgame);
private:
	vector<Person> player;
	static Game* plast;
	Game(const Game&); // Don't copy (no need)
	Game& operator =(const Game&); // Don't assign
};
Now implementation:
Code:
/// Implementation.
namespace {
	Game nullgame(0,0);
}

string Person::toString() const
{
	strstream s;
	s << name << ":{";
	for (int i = 0; i < scores.size(); ++i)
		s << " " << scores[i];
	s << "}" << ends;
	return s.str();
}

Game* Game::plast = &nullgame;

Game::~Game()
{
	if (plast == this)
		plast = &nullgame;
}

Game* Game::setDefault(Game* pgame)
{
	Game* oldp = defined()?plast:0;
	plast = pgame?pgame:&nullgame;
	return oldp;
}

Game* Game::setDefault(Game& game)
{
	return setDefault(&game);
}

bool Game::none()
{
	return plast == &nullgame;
}

int Game::addPlayer(Person& newp)
{
	player.push_back(newp);
	return size() - 1;
}

void Game::resize(int np, int ns)
{
	if (np > 0)
	{
		Person p(ns > 0?ns:player[0].scores.size());
		player.resize(np,p);
	}
	if (ns > 0)
	{
		for (int i = 0; i < player.size(); ++i)
			player[i].scores.resize(ns);
	}
}
See (minimal;) test drive:
Code:
void TestDftGame()
{
	cout << "Test global access:" << endl;
	if (Game::defined())
	{
		cout << Game::last()[0].toString() << endl;
	}
	Game& game = Game::last(); // Use it in functions...
	cout << game[0].toString() << endl;
}

int main(int argc, char* argv[])
{
	Game	game(3,5);
	cout << game.size() << endl;
	cout << game[0].scores.size() << endl;
	Person& p = game[0];
	p.name = "1st";
	p.scores[0] = 2005;
	cout << p.toString() << endl;
	game.resize(2,3);
	cout << game.size() << endl;
	cout << game[0].toString() << endl;

	TestDftGame();

	return 0;
}
Don't forget to include headers:
Code:
#include <iostream>
#include <strstream>
#include <string>
#include <vector>
It's not a minimal solution (but it's not a true professional solution - for example, it has only minimal error handling, no game objects count etc).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top