Unable to initialize values

S

steveneng

C++ Primer Plus Programming Exercises 4th Ed - Prate

Help I'm trying to refresh myself and I'm stuck on this problem (not
homework/school related but for personal advancement).

6: Do programming exercise 3, but, instead of declaring an array of
three CandyBar structures, use new to allocate the array dynamically.

That's the question in case you guys don't ahve the book. I have
Exercises 3 done and I can properly use structures. But I'm not sure
how to use it for 6. Here's problem 3 solved



code:--------------------------------------------------------------------------------

// C++ Primer Plus
// chapter 4
// Programming Exercises
// 3: The CandyBar structure contains three members, as described in
Programming Exercise 2. Write a program that creates an array of three
CandyBar
// structures, initializes them to values of your choice, and then
displays the contents of each structure.

// Author: Steven C. Eng
// Last Revisited: 12.30.03

#include <iostream>
using namespace std;

struct CandyBar
{
char brandname[20];
double weight;
int calories;
};

int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Chocolate Hurricane", 2.4, 400},
{"Hersey", 2.5, 450}
};

cout << "Brand name of candy bar: " << snack[0].brandname << "\n";
cout << "Weight of candy bar: " << snack[0].weight << "\n";
cout << "Calories of candy bar: " << snack[0].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[1].brandname << "\n";
cout << "Weight of candy bar: " << snack[1].weight << "\n";
cout << "Calories of candy bar: " << snack[1].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[2].brandname << "\n";
cout << "Weight of candy bar: " << snack[2].weight << "\n";
cout << "Calories of candy bar: " << snack[2].calories << "\n";
}

--------------------------------------------------------------------------------


I use this statement to use new to create an array dynamically but I'm
still having trouble initialize them.

CandyBar * snack = new Candybar[3];

I'm kinda stuck. any help would be appreciated. This book goes over
using new wiht arrays and new with structures but I'm not quite sure
how to bring the two together. Thanks.
 
S

St3vie

Hi, I don't know if this is a mis-type in your message or
if it's really in the code, but 'Candybar' with a small 'b' might be wrong
in this line you use:

CandyBar * snack = new Candybar[3];

Don't know if this is it...
-St3vie
 
T

Thomas Wintschel

C++ Primer Plus Programming Exercises 4th Ed - Prate

Help I'm trying to refresh myself and I'm stuck on this problem (not
homework/school related but for personal advancement).

6: Do programming exercise 3, but, instead of declaring an array of
three CandyBar structures, use new to allocate the array dynamically.

That's the question in case you guys don't ahve the book. I have
Exercises 3 done and I can properly use structures. But I'm not sure
how to use it for 6. Here's problem 3 solved



code:-----------------------------------------------------------------
---------------

// C++ Primer Plus
// chapter 4
// Programming Exercises
// 3: The CandyBar structure contains three members, as described in
Programming Exercise 2. Write a program that creates an array of three
CandyBar
// structures, initializes them to values of your choice, and then
displays the contents of each structure.

// Author: Steven C. Eng
// Last Revisited: 12.30.03

#include <iostream>
using namespace std;

struct CandyBar
{
char brandname[20];
double weight;
int calories;
};

int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Chocolate Hurricane", 2.4, 400},
{"Hersey", 2.5, 450}
};

cout << "Brand name of candy bar: " << snack[0].brandname << "\n";
cout << "Weight of candy bar: " << snack[0].weight << "\n";
cout << "Calories of candy bar: " << snack[0].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[1].brandname << "\n";
cout << "Weight of candy bar: " << snack[1].weight << "\n";
cout << "Calories of candy bar: " << snack[1].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[2].brandname << "\n";
cout << "Weight of candy bar: " << snack[2].weight << "\n";
cout << "Calories of candy bar: " << snack[2].calories << "\n";
}

-------------------------------------------------------------------- ------------


I use this statement to use new to create an array dynamically but I'm
still having trouble initialize them.

CandyBar * snack = new Candybar[3];

I'm kinda stuck. any help would be appreciated. This book goes over
using new wiht arrays and new with structures but I'm not quite sure
how to bring the two together. Thanks.

Your call to new allocates the three objects as desired. Now you need
to initialize them, e.g.

strcpy(snack[0].brandname, "Mocha Munch");
snack[0].weight = 2.3;
snack[0].calories = 350;

Tom
 
T

Thomas Matthews

C++ Primer Plus Programming Exercises 4th Ed - Prate

Help I'm trying to refresh myself and I'm stuck on this problem (not
homework/school related but for personal advancement).

6: Do programming exercise 3, but, instead of declaring an array of
three CandyBar structures, use new to allocate the array dynamically.

That's the question in case you guys don't ahve the book. I have
Exercises 3 done and I can properly use structures. But I'm not sure
how to use it for 6. Here's problem 3 solved [snip]

struct CandyBar
{
char brandname[20];
double weight;
int calories;
};

int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Chocolate Hurricane", 2.4, 400},
{"Hersey", 2.5, 450}
}; [snip]

--------------------------------------------------------------------------------


I use this statement to use new to create an array dynamically but I'm
still having trouble initialize them.

CandyBar * snack = new Candybar[3];

I'm kinda stuck. any help would be appreciated. This book goes over
using new wiht arrays and new with structures but I'm not quite sure
how to bring the two together. Thanks.

strcpy(snack[0].brandname, "Mocha Munch");
snack[0].weight = 2.3;
snack[0].calories = 350;

The next step might be to create a constructor method for your
structure:
struct CandyBar
{
CandyBar(const char * new_brand_name,
double new_weight,
int new_calories)
{
strcpy(brandname, new_brand_name);
weight = new_weight;
calories = new_calories;
}
CandyBar()
: brandname(NULL), weight(0.0), calories(0)
{ ; }
// ....
};
Now you can use the constructor to initialize the candy bars:
snack[0] = CandyBar("Mocha Munch", 2.3, 350);

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jeff Schwab

Thomas said:
C++ Primer Plus Programming Exercises 4th Ed - Prate

Help I'm trying to refresh myself and I'm stuck on this problem (not
homework/school related but for personal advancement).

6: Do programming exercise 3, but, instead of declaring an array of
three CandyBar structures, use new to allocate the array
dynamically.

That's the question in case you guys don't ahve the book. I have
Exercises 3 done and I can properly use structures. But I'm not sure
how to use it for 6. Here's problem 3 solved




code:-----------------------------------------------------------------
---------------

// C++ Primer Plus
// chapter 4
// Programming Exercises
// 3: The CandyBar structure contains three members, as described in
Programming Exercise 2. Write a program that creates an array of
three

CandyBar
// structures, initializes them to values of your choice, and then
displays the contents of each structure.

// Author: Steven C. Eng
// Last Revisited: 12.30.03

#include <iostream>
using namespace std;

struct CandyBar
{
char brandname[20];
double weight;
int calories;
};

int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Chocolate Hurricane", 2.4, 400},
{"Hersey", 2.5, 450}
};

cout << "Brand name of candy bar: " << snack[0].brandname << "\n";
cout << "Weight of candy bar: " << snack[0].weight << "\n";
cout << "Calories of candy bar: " << snack[0].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[1].brandname << "\n";
cout << "Weight of candy bar: " << snack[1].weight << "\n";
cout << "Calories of candy bar: " << snack[1].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[2].brandname << "\n";
cout << "Weight of candy bar: " << snack[2].weight << "\n";
cout << "Calories of candy bar: " << snack[2].calories << "\n";
}

--------------------------------------------------------------------
------------


I use this statement to use new to create an array dynamically but
I'm

still having trouble initialize them.

CandyBar * snack = new Candybar[3];

I'm kinda stuck. any help would be appreciated. This book goes over
using new wiht arrays and new with structures but I'm not quite sure
how to bring the two together. Thanks.


Your call to new allocates the three objects as desired. Now you need
to initialize them, e.g.

strcpy(snack[0].brandname, "Mocha Munch");
snack[0].weight = 2.3;
snack[0].calories = 350;

Tom

Tom,

I'm sure that's exactly what the OP needed. I would like to clarify
though, for the sake of the OP, that what you've shown is not actually
initialization, it's assignment.

Steven,

When an object (like one of your CandyBars) is created with a particular
value, it is said to have been "initialized" to that value. When the
object already has been constructed, and you replace its value, you have
"assigned" to it.

Initializing a dynamically created array, so that each object has a
particular, unique value, is not trivial. The approach Tom showed is
the usual work-around.

I am not familiar with the "C++ Primer Plus Programming Exercises," but
it seems the author has been sloppy with terminology. This link might
help explain the difference between initialization and assignment:

http://www.idt.mdh.se/~icc/winsde/CppStyleGuide/CPPGuideline.htm#Assignment vs. initialization

By the way, you don't actually have to work with low-level arrays of
characters just to store strings; the C++ standard library includes a
string type. Also, when you need to perform an action (like printing
the contents of a CandyBar object) several times, it may be more
convenient to define a function you can call as needed. Here's an
example using a standard string to replace the low-level character
array, and a function to format the output of your CandyBar objects.
Good luck, and happy coding!

-Jeff

// C++ Primer Plus
// chapter 4
// Programming Exercises

// 3: The CandyBar structure contains three members, as described in
// Programming Exercise 2. Write a program that creates an array of
// three CandyBar structures, initializes them to values of your choice,
// and then displays the contents of each structure.

// Author: Steven C. Eng
// Last Revisited: 12.30.03

#include <iostream>

struct CandyBar
{
std::string brandname;
double weight;
int calories;
};

std::eek:stream& operator << ( std::eek:stream& out, CandyBar const& bar )
{
return out << "Brand name of candy bar: " << bar.brandname
<< "\nWeight of candy bar: " << bar.weight
<< "\nCalories of candy bar: " << bar.calories
<< "\n\n";
}

int main( )
{
CandyBar snack[ 3 ] =
{
{ "Mocha Munch", 2.3, 350 },
{ "Chocolate Hurricane", 2.4, 400 },
{ "Hersey", 2.5, 450 }
};

std::cout << snack[ 0 ]
<< snack[ 1 ]
<< snack[ 2 ];

CandyBar* more_snacks = new CandyBar[ 3 ];

std::cout << more_snacks[ 0 ] // These CandyBars got only
<< more_snacks[ 1 ] // only the default
<< more_snacks[ 2 ]; // initialization.
}
 
J

jbruno4000

Subject: Unable to initialize values
From: (e-mail address removed)
Date: 1/3/2004 10:22 AM Pacific Standard Time
Message-id: <[email protected]>

C++ Primer Plus Programming Exercises 4th Ed - Prate

Help I'm trying to refresh myself and I'm stuck on this problem (not
homework/school related but for personal advancement).

6: Do programming exercise 3, but, instead of declaring an array of
three CandyBar structures, use new to allocate the array dynamically.

That's the question in case you guys don't ahve the book. I have
Exercises 3 done and I can properly use structures. But I'm not sure
how to use it for 6. Here's problem 3 solved




code:-------------------------------------------------------------------- ------------

// C++ Primer Plus
// chapter 4
// Programming Exercises
// 3: The CandyBar structure contains three members, as described in
Programming Exercise 2. Write a program that creates an array of three
CandyBar
// structures, initializes them to values of your choice, and then
displays the contents of each structure.

// Author: Steven C. Eng
// Last Revisited: 12.30.03

#include <iostream>
using namespace std;

struct CandyBar
{
char brandname[20];
double weight;
int calories;
};

int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Chocolate Hurricane", 2.4, 400},
{"Hersey", 2.5, 450}
};

cout << "Brand name of candy bar: " << snack[0].brandname << "\n";
cout << "Weight of candy bar: " << snack[0].weight << "\n";
cout << "Calories of candy bar: " << snack[0].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[1].brandname << "\n";
cout << "Weight of candy bar: " << snack[1].weight << "\n";
cout << "Calories of candy bar: " << snack[1].calories << "\n";

cout << "\n";

cout << "Brand name of candy bar: " << snack[2].brandname << "\n";
cout << "Weight of candy bar: " << snack[2].weight << "\n";
cout << "Calories of candy bar: " << snack[2].calories << "\n";
}


------------------------------------------------------------------------- -------


I use this statement to use new to create an array dynamically but I'm
still having trouble initialize them.

CandyBar * snack = new Candybar[3];

I'm kinda stuck. any help would be appreciated. This book goes over
using new wiht arrays and new with structures but I'm not quite sure
how to bring the two together. Thanks.

I think a more efficient way to output the data from exercise 3 might be:

for(int i = 0; i < 3; i++)
{
cout << "Brand name of candy bar: " << snack.brandname << "\n";
cout << "Weight of candy bar: " << snack.weight << "\n";
cout << "Calories of candy bar: " << snack.calories << "\n";
cout << "\n";
}

It's easier than writing output statements for each object in your array. I
know it's only 3 items but it could just as easily have been 5 or 20 or 100.

If you want to use 'new' to create an array dynamically, this tells me you have
no idea how many CandyBar objects will be created. You need to set up an
algorithm that solves your problem, i.e.

while(data)
{
create new object;
assign values to object contents;
count++;
cin >> data;
}

The size of your array will be 'count' not 3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top