I need help with this problem. The assignment says: "Do programming Exercise 6, but instead of declaring an array of three CandyBar structure, use new to allocate the array dynamically".

Here is assignment 6:

#include <iostream>

using namespace std;

struct CandyBar {

char candyName[25];
double candyAmount;
int twoCandyWeight;


};

int main(){
	
	
	CandyBar detailOfCandy[3] = {
	{"Donkey",
	2.5,
	200},

	{"Monkey", 4.4, 100},

	{"Honkey", 3.3, 50}

	
	
	
	
	
	};

	cout << "First CandyBar: " << detailOfCandy[0].candyName << "Second CandyBar: "<<detailOfCandy[1].candyName <<" Third CandyBar: " 
	<< detailOfCandy[2].candyName;
	

cin.get();
cin.get();
return 0;
}

How do I make this happend without ruinning all that "beautifull code"? Or atleast give me direction/explation for it please :). Hope someone can help me

Recommended Answers

All 5 Replies

Try this:

int main()
{
 CandyBar *detailOfCandy;
 detailsOfCandy=new CandyBar[3];

 strcpy((detailOfCandy+0)->candyName,"Donkey");   // include header file string.h
 (detailOfCandy+0)->candyAmount=2.5;
 (detailOfCandy+0)->twoCandyWeight=200;

 strcpy((detailOfCandy+0)->candyName,"Monkey");
 (detailOfCandy+0)->candyAmount=4.4;
 (detailOfCandy+0)->twoCandyWeight=100;

 strcpy((detailOfCandy+0)->candyName,"Honkey");
 (detailOfCandy+0)->candyAmount=3.3;
 (detailOfCandy+0)->twoCandyWeight=50;

 // use rest of code
 ...........
 ...........
}

Wow m8 thats seems a bit too advanced for me :D. I appreciate your time and help no doubt. But im sure it can be done much more simple.. Or maybe it cannot?

oh come on, it cant get any simpler than this.
Just refer to the code snippet given, you should do fine

Ok ty guys... Thread solved

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.