473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unable to initialize values

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.
Jul 22 '05 #1
6 2548
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

<st*******@earthlink.net> wrote in message
news:pv********************************@4ax.com...
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.

Jul 22 '05 #2

<st*******@earthlink.net> wrote in message
news:pv********************************@4ax.com...
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
Jul 22 '05 #3
st*******@earthlink.net wrote:
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

Jul 22 '05 #4
Thomas Wintschel wrote:
<st*******@earthlink.net> wrote in message
news:pv********************************@4ax.com...
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/Cp...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::ostream& operator << ( std::ostream& 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.
}

Jul 22 '05 #5
Thanks guys. I understand now.
Jul 22 '05 #6
>Subject: Unable to initialize values
From: st*******@earthlink.net
Date: 1/3/2004 10:22 AM Pacific Standard Time
Message-id: <pv********************************@4ax.com>

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[i].brandname << "\n";
cout << "Weight of candy bar: " << snack[i].weight << "\n";
cout << "Calories of candy bar: " << snack[i].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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
0
by: Henry | last post by:
I have written an ASP/VB.Net application via VS 2003 (Crystal V9) that uses MS Access 2000 as its database. I can export reports that have no linked sub reports for printing. However, I'm unable...
1
by: Pepe | last post by:
Hi, I have created the "Hello World" web service with one web method on it "HelloWorld" (yes the tag is there). I have deployed the web service to a Windows 2003 server, then I create a new...
1
by: cindy | last post by:
I am using this link to create a web service to WSS Library http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_SP2003_ta/html/ODC_WritingCustomWebServicesforSPPT.asp The web...
0
by: genc ymeri | last post by:
Hi over there, I'm building a webservice (C#/>Net 1.1) and I'm trying to add a web reference , a WSDL file from a location "c:\inetpub\WSDLfile.wsdl" . (This is the WSDL file of another webserver...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
4
by: =?Utf-8?B?R3JhbmRtYSBDYXJvbA==?= | last post by:
Can anyone tell me what the following message mean? "cannot initialize application, unable to open file {15034:1:8] I am getting this message when I try to open a video software from my desktop....
0
by: geeteshss | last post by:
the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code what should i do...earlier i could view it also below this code is...
4
by: Bill Nicholson - Cincinnati, OH | last post by:
This Access app is in Access 2000 format. It is a mature app that is running on machines all over the LAN. Today when I tried to put it on a new PC with a fresh copy of Win XP and Access 2003. XP...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.