Structures and Classes giving me problems. This program simply calculates volume of a CBox class.
-
Hello all, I am working on my homework and I am having issues. When I added a third box to the equation, the total volume in bytes went from 24bytes down to 8bytes. This is not right, when adding another box to my program it should increase in size. But all I had to do was add a third box and calculate the Length, Width, and Height of the third added box. Then calculate the total volume for all three boxes. Sounds very simple but after adding my third box and changing the code a little bit, I am having issues. I do not understand why it is not adding the two boxVolume1 + boxVolume3 together and then displaying it. The source is listed below. Thank you all to can contribute to pointing out why this program is not working correctly.
#include <iomanip>
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox box1; // Declare box1 of type CBox
CBox box2; // Declare box2 of type CBox
CBox box3; // Declare box3 of type CBox
double boxVolume1 = 0.0; // Stores the volume of a box1
double boxVolume3 = 0.0; // Stores the volume of a box3
double volumetotal = 0.0;
box1.m_Height = 18.0; // Define the values
box1.m_Length = 78.0; // of the members of
box1.m_Width = 24.0; // the object box1
box2.m_Height = box1.m_Height - 10; // Define box2
box2.m_Length = box1.m_Length/2.0; // members in
box2.m_Width = 0.25*box1.m_Length; // terms of box1
box3.m_Height = 108.0; // Define the values
box3.m_Length = 32.0; // of the members of
box3.m_Width = 18.0; // the object box3
// Calculate volume of box1 and box3
boxVolume1 = box1.m_Height*box1.m_Length*box1.m_Width;
boxVolume3 = box3.m_Height*box3.m_Length*box3.m_Width;
cout << endl
<< "Volume of box1 = " << boxVolume1;
cout << endl
<< "Volume of box3 = " << boxVolume3;
cout << endl
<< "box2 has sides which total "
<< box2.m_Height+ box2.m_Length+ box2.m_Width
<< " inches.";
// Calculate total volume of all boxes
volumetotal = boxVolume1+boxVolume3;
cout << volumetotal;
cout << endl // Display the size of a box in memory
<< "A CBox object occupies "
<< sizeof volumetotal << " bytes.";
cout <<endl;
return 0;
} -
Hello all, I am working on my homework and I am having issues. When I added a third box to the equation, the total volume in bytes went from 24bytes down to 8bytes. This is not right, when adding another box to my program it should increase in size. But all I had to do was add a third box and calculate the Length, Width, and Height of the third added box. Then calculate the total volume for all three boxes. Sounds very simple but after adding my third box and changing the code a little bit, I am having issues. I do not understand why it is not adding the two boxVolume1 + boxVolume3 together and then displaying it. The source is listed below. Thank you all to can contribute to pointing out why this program is not working correctly.
#include <iomanip>
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox box1; // Declare box1 of type CBox
CBox box2; // Declare box2 of type CBox
CBox box3; // Declare box3 of type CBox
double boxVolume1 = 0.0; // Stores the volume of a box1
double boxVolume3 = 0.0; // Stores the volume of a box3
double volumetotal = 0.0;
box1.m_Height = 18.0; // Define the values
box1.m_Length = 78.0; // of the members of
box1.m_Width = 24.0; // the object box1
box2.m_Height = box1.m_Height - 10; // Define box2
box2.m_Length = box1.m_Length/2.0; // members in
box2.m_Width = 0.25*box1.m_Length; // terms of box1
box3.m_Height = 108.0; // Define the values
box3.m_Length = 32.0; // of the members of
box3.m_Width = 18.0; // the object box3
// Calculate volume of box1 and box3
boxVolume1 = box1.m_Height*box1.m_Length*box1.m_Width;
boxVolume3 = box3.m_Height*box3.m_Length*box3.m_Width;
cout << endl
<< "Volume of box1 = " << boxVolume1;
cout << endl
<< "Volume of box3 = " << boxVolume3;
cout << endl
<< "box2 has sides which total "
<< box2.m_Height+ box2.m_Length+ box2.m_Width
<< " inches.";
// Calculate total volume of all boxes
volumetotal = boxVolume1+boxVolume3;
cout << volumetotal;
cout << endl // Display the size of a box in memory
<< "A CBox object occupies "
<< sizeof volumetotal << " bytes.";
cout <<endl;
return 0;
}rbwest86 wrote:
sizeof volumetotal
sizeof volumetotal
is always8
whatever value it contains (in the range allowed fordouble
s, of course, see [^]). Adding values tovolumetotal
cannot change its size in memory (it changes just the bit pattern in memory). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
rbwest86 wrote:
sizeof volumetotal
sizeof volumetotal
is always8
whatever value it contains (in the range allowed fordouble
s, of course, see [^]). Adding values tovolumetotal
cannot change its size in memory (it changes just the bit pattern in memory). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
is always 8 a constant, often 8
FTFY The good old Data General machines had 36-bit words holding five 7-bit bytes plus a flag bit, so they would store a double in slightly more than 10 bytes. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
-
CPallini wrote:
is always 8 a constant, often 8
FTFY The good old Data General machines had 36-bit words holding five 7-bit bytes plus a flag bit, so they would store a double in slightly more than 10 bytes. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes. ;P
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes. ;P
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes.
If that is true, you've just proven yourself not to be a Klingon developer. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
-
CPallini wrote:
Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes.
If that is true, you've just proven yourself not to be a Klingon developer. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.