954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help passing an array of structs to a function?

What i am trying to do is pass an array of structs to a function.

the code i have so far follows:

struct Contribution
{
	string name;
	double amount;
};

const int MAX = 25;

typedef Contribution BigSpender;
typedef BigSpender ContributionRec[MAX];

const string NAME_FILE = "names.txt";
const string CONTRIBUTION_FILE = "slush.txt";

void InitProgram( BigSpender ContributionRec[MAX] );

int main()
{

	InitProgram( ContributionRec[MAX] );
	return 0;
}

void InitProgram( BigSpender& ContributionRec[MAX] )
{
	return;
}

Can anyone tell me what I am doing wrong?

Skeezo
Newbie Poster
11 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
struct Contribution
{
	string name;
	double amount;
};

const int MAX = 25;

typedef Contribution BigSpender[MAX];

const string NAME_FILE = "names.txt";
const string CONTRIBUTION_FILE = "slush.txt";

void InitProgram( BigSpender& ContributionRec );

int main()
{
    BigSpender ContributionRec;
	InitProgram( ContributionRec );
	return 0;
}

void InitProgram( BigSpender& ContributionRec )
{
	return;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thanks!

Skeezo
Newbie Poster
11 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

like i said before thank you, but now when i run the code i get this message

'BigSpender' does not define this operator or a conversion to a type acceptable to the predefined operator

struct Contribution
{
	string name;
	double amount;
};

const int MAX = 25;

typedef Contribution BigSpender;
typedef BigSpender ContributionRec[MAX];

const string NAME_FILE = "names.txt";
const string CONTRIBUTION_FILE = "slush.txt";

void InitProgram(BigSpender& ContributionRec);

int main()
{
BigSpender ContributionRec;

	InitProgram(ContributionRec);
	return 0;
}

void InitProgram(BigSpender& ContributionRec)
{
ifstream nameLog, contributionLog;

nameLog.open( NAME_FILE.c_str() ); // open file
contributionLog.open( CONTRIBUTION_FILE.c_str() ); // open file

	for ( int i = 0; i < MAX; i++)
	{
	nameLog >> ContributionRec[i].name;
	cout << names << endl;
	}

	nameLog.close();
	contributionLog.close();

	return;
}
Skeezo
Newbie Poster
11 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

delete lines 9 and 10 because that doesn't work, and replace it with just this one line
typedef Contribution BigSpender[MAX];

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thanks

Skeezo
Newbie Poster
11 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Are all the typdef's really needed? I find it hard to follow the bouncing, er, whatever.

struct Contribution
{
	string name;
	double amount;
};

const int MAX = 25;

const string NAME_FILE = "names.txt";
const string CONTRIBUTION_FILE = "slush.txt";

void InitProgram( Contribution contrib[] );

int main()
{
    Contribution  ContributionRec[MAX];
	InitProgram( ContributionRec );
	return 0;
}

void InitProgram( Contribution contrib[] )
{
	return;
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You