I am learning C++ using Stephen Pratas "C++ Primer Plus" (excellent book btw). However i am stuck on exercise 6 chapter 4, where in essence i just have to use new to allocate the array of structures dynamically. I can allocate a single structure dynamically, however i still do not know how to initialise all the members at one time, only each member seperately by using cin e.g. i can only do:

cin.get(structure1->name, 20);

and i do not know how to initialise a whole dynamically allocated structure like i can with a normal 1 e.g. i cant do this:

structure1 object1 =
{
"Balh blah blah".
1.88.
5
}

Can please someone help. thnx in advance

Recommended Answers

All 5 Replies

structure object1 = {1,2,3,4,5,6,etc};
structure object2={"something","anything", "another thing"};

Now if u wanted "something" from object 2 to correspond to a number from object one you can use parralell arrays.

But a matrix would be better.

Ok--try this (i'm not sure it's perfect...13 yr olds can make mistakes)

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct nod
{
int info;
nod *adr;
};
nod *vf,*sf;// *vf-is the the first element in the list
// *sf- is the last element
void add(int val,nod* &vf)
{
if (!vf) //lista este goala
{
vf=(nod*)malloc(sizeof(nod));
vf->info=val;
sf=vf;
}
else
{
nod *c=(nod*)malloc(sizeof(nod));
c->info=val;
c->adr=NULL;
sf->adr=c;
sf=c;
}
}
void list(nod *vf)
{
nod *c;
c=vf;
while (c)
{
printf("%i ",c->info);
c=c->adr;
}
}
/*void insert(int vc,int vi,nod *vf)
{
nod *c;
c=vf;
while (c->info!=vc) c=c->adr;
nod *e=(nod*)malloc(sizeof(nod));
e->info=vi;
e->adr=c->adr;
c->adr=e;
}*/ //it inserts a nod in the list after looking for a value-vc
// you don't need the insert(...) function
void main()
{
clrscr();
int n;//it reads the numer of elements which will be in the list
printf("n=");scanf("%i",&n);
for (int i=0;i<n;i++)
{
int a;//value you read-- i assumed it was a list
printf("data:");// with int information-and nothing else
scanf("%d",&a);
add(a,vf);
list(vf);//writes the list on stdout (screen)
}

Hope this helps :cheesy:

Not sure I understand the request, but using new for dynamic arrays goes like this:

void CreateArray( int numberToCreate )
{
// this allocates space and calls the constructor on each element.
AnObject* theDynamicArray = new AnObject[ numberToCreate ];

// example of use
for (int i = 0; i < numberToCreate; i++)
DoSomething( theDynamicArray[ i ] ); // gets passed as AnObject, not AnObject*

delete [] theDynamicArray; // destructor of each element gets called.
}

The constructor would be responsible for initializing the object, of course, but if you want to load some initial data in from the console or a resource or something, you'd have to do that post-construction. Like, in the DoSomething() routine. Also, a routine like DoSomething() should take AnObject as a reference so it can be changed (and so it doesn't need to be copied by the compiler).

I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise. I sorted that out and I'm posting my source code so you may continue with the following chapters. If someone can come up with a nicer or more elegant solution please post.

#include <iostream>
using namespace std;


int main()
{
struct CandyBar //struct declaration
{
char *brand;
float weight;
int calories;
};


//dynamic array declaration
CandyBar *Candies = new CandyBar[3];
CandyBar *CandyPointer = &Candies[0];  //pointer to a CanyBar data object


//structure initialization one by one
(*CandyPointer).brand = "Mocha Munch";
CandyPointer->weight = 2.3;
CandyPointer->calories = 350;


CandyPointer = &Candies[1];
(*CandyPointer).brand = "Chilly Willy";
CandyPointer->weight = 2.2;
CandyPointer->calories = 200;


CandyPointer = &Candies[2];
(*CandyPointer).brand = "Lemon Rush";
CandyPointer->weight = 2.1;
CandyPointer->calories = 200;


//data output to the screen


//first element of the array
CandyPointer = &Candies[0];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";


//second element of the array
CandyPointer = &Candies[1];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";


//third element of the array
CandyPointer = &Candies[2];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";


cin.get();
return 0;
}

>I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise.
This thread is two *years* old. If you have a question, start a new thread rather than resurrect one that's already begun to decay. We can't stand the smell of necrothreads.

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.