Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

randomDecay

Displaying the contents of a structure?

This topic is 6179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Is there a way to display the contents of a structure. Like, would I have to use a pointer? Here''s the code...I want to display the value of the snack variable (which is of type CandyBar):
  

// Structures

struct CandyBar
{
	// Brand name of candy bar

	char brandName[arSize];

	// Weight of candy bar ( could be fractional )

	float weight;

	// Number of calories

	int calories;
};

CandyBar snack =
{
   "Mocha Munch",
   2.3f,
   350

};

  

Share this post


Link to post
Share on other sites
Advertisement
cout << snack.brandName << " has " << snack.calories << " per " << snack.wieght << " grams." << endl; 




To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy

Share this post


Link to post
Share on other sites
you could put a toString() function if you use classes... otherwise i think you have to do it member-by-member...

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
how mush easier do you want it ?


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites

struct CandyBar
{
// struct access public by default
// other declarations...
friend ostream &operator<< (ostream &os, CandyBar &bar);

private:
char brandName[arSize];
float weight;
int calories;
};

ostream &operator<< (ostream &os, CandyBar &bar)
{
// note we do not append endl. leave that to calling routine
os << brandName << " has " << calories << " calories per " << weight << " grams.";
}

You can now write:

CandyBar g = {/*fill in initialization*/};
cout << g << endl;




To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!