Archived

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

Displaying the contents of a structure?

This topic is 5894 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

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