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] = {

	{"Monkey", 4.4, 100},

	{"Honkey", 3.3, 50}


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

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

Try this:

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

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



 // use rest of code

Edited 5 Years Ago by chikkupa: n/a

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?

This question has already been answered. Start a new discussion instead.