Have you ever had a problem where you are completely dumbfounded? Staring at the output of a program and saying this is impossible? Sort of like…
int a =5;
printf(“a=%d\n”,a);
Output:
A=6
Well, I’d appreciate if someone would take a look at this simple program below which creates a class Cbox that has just length, width, and height private double members, and a volume function to return the volume. Then I create an array of Cboxes named CArray. Oh, the Cbox class has a display() member function to output the three sides and the volume. That is important because that is where the screw up is occurring, i.e., when I try to call the display() function either on an individual Cbox object, or in a loop to dump all Cboxes from the array. Just to give you an idea of the kind of corruption I’m seeing before I show you the program, here is a little output:
m_Length = 2.000000 m_Width = 3.000000 m_Height = 1.000000 volume() = 2181932029977806200
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000
m_Length = 2.200000 m_Width = 1.000000 m_Height = 0.500000 volume() = 1200062616487793500
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000
m_Length = 0.200000 m_Width = 3.500000 m_Height = 2.000000 volume() = 4363864059955613000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000
m_Length = 0.300000 m_Width = 3.000000 m_Height = 3.000000 volume() = 9818694133995429100
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000
[/CODE=C++]
I think you get the idea.
I’ve run this program on two different computers using three different compilers/development environments, i.e., Microsoft Visual C++ 6.0, Dev C++, and the new CodeBlocks. The program works perfectly in Dev C++ and CodeBlocks, but screws up badly in VC++. Its almost like it can’t handle four %f format specifiers in a row.
The output should look like this…
[CODE=C++]
CArray() Constructor Called!
2.000000 3.000000 1.000000 6.000000
2.200000 1.000000 0.500000 1.100000
0.200000 3.500000 2.000000 1.400000
0.300000 3.000000 3.000000 2.700000
2.000000 3.000000 1.000000 6.000000
2.200000 1.000000 0.500000 1.100000
0.200000 3.500000 2.000000 1.400000
0.300000 3.000000 3.000000 2.700000
[/CODE=C++]
Here is the program. Could somebody run it and see what they get, and if it screws up give me an idea what’s wrong (it’s a really short, simple program)?
[CODE=C++]
#include <stdio.h>
#define MAX_SIZE 5
class CBox
{
public:
CBox(double lv=1.0, double wv=1.0, double hv=1.0)
:m_Length(lv), m_Width(wv), m_Height(hv)
{
//printf("CBox() Constructor Called!\n");
}
~CBox()
{
//printf("~CBox() Destructor Called!\n");
}
void SetLength(double ln)
{
this->m_Length=ln;
}
void SetWidth(double wd)
{
this->m_Width=wd;
}
void SetHeight(double ht)
{
this->m_Height=ht;
}
double volume(void)
{
return this->m_Length * this->m_Width * this->m_Height;
}
void display(void)
{
printf("%f\t%f\t%f\t%f\n",m_Length,m_Width,m_Height,volume());
}
private:
double m_Length;
double m_Width;
double m_Height;
};
class CArray
{
public:
CArray()
{
printf("CArray() Constructor Called!\n");
m_New=0;
}
~CArray()
{
printf("CArray() Destructor Called!\n");
}
void add(CBox& box)
{
if(m_New<MAX_SIZE)
{
bx[m_New]=box;
m_New++;
}
}
void dump(void)
{
unsigned int i;
for(i=0;i<m_New;i++)
bx[i].display();
}
private:
CBox bx[MAX_SIZE];
unsigned int m_New;
};
int main(void)
{
unsigned int i;
CArray arr;
CBox bx[4];
bx[0].SetHeight(1.0F), bx[0].SetLength(2.0F), bx[0].SetWidth(3.0F);
bx[1].SetHeight(0.5F), bx[1].SetLength(2.2F), bx[1].SetWidth(1.0F);
bx[2].SetHeight(2.0F), bx[2].SetLength(0.2F), bx[2].SetWidth(3.5F);
bx[3].SetHeight(3.0F), bx[3].SetLength(0.3F), bx[3].SetWidth(3.0F);
for(i=0;i<4;i++)
{
arr.add(bx[i]);
bx[i].display();
}
printf("\n\n");
arr.dump();
getchar();
return 0;
}