473,837 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2590
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*******@eart hlink.net> wrote in message
news:pv******** *************** *********@4ax.c om...
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*******@eart hlink.net> wrote in message
news:pv******** *************** *********@4ax.c om...
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*******@earth link.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(brandnam e, 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.l earn.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*******@eart hlink.net> wrote in message
news:pv******** *************** *********@4ax.c om...
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 "initialize d" 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*******@earth link.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
38262
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 global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
0
2729
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 to export reports that have linked subreports. I receive (a "Missing parameter field current value") on the following statement: Me.crReportDocument.Export() The main report requires 4 parameters. The linked reports do not require any...
1
5072
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 project in my local pc and try to add a web reference to the "Hello World" web service and I get the warning message: Custom tool warning: DiscoCodeGenerator unable to initialize code generator. No code generated.
1
1906
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 service is register and can be found by a test web application. I use the wsdl and get No Ports or Methods were found on this page. If this is an ASP.NET Web service, make sure that all WebMethods are public and have a <WebMethod> attribute.
0
1648
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 build in Java/Apache/linux .....which our project will integrate with) What happens is that the reference get added, I can read the XML in the wizard window, button Add Reference is enabled...... but once I add it, not wrapper code of the proxy...
12
5826
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
3409
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. My operating system: Microsoft Windows XP Home edition version 2002 service pack 2 Graphic card: NVIDIA GeFORCE FX5200 Sound card: SB Live! Audio Processor: Intel (R) Pentium (R)4 CPU 3.00 GHz 300 GHz,512MB of RAM thank you
0
1401
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 the code which is in my html page plz help........ private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { BindData(); }
4
21356
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 has all patches installed and is SP 2. I got this error: Can't be started. Was unable to initialize the Windows Registry. Rerun Microsoft Access or Microsoft Office Setup to reinstall I ran the app as domain admin and local admin, no change.
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10267
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.