Volume Computation Gone Wrong!

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Box
{
    private:
        int Width;
        int Depth;
        int Height;
        int Volume;
        int minX;
        int maxX;
        int maxY;
        int minY;
        string type;
		int L1;
		int L2;
		int L3;
		int L4;

    public:
	Box()
        {
            int Boxes[5];
	    Boxes[0]=0;
            Boxes[1]=1;
            Boxes[2]=2;
            Boxes[3]=3;
            Boxes[4]=4;
        }
        int getDepth();
        int getWidth();
	int getHeight();
	int getVolume();
        int getMaxX();
        int getMaxY();
        int getMinX();
        int getMinY();
        string getType();
        void printData();
		void print1Box();
		void setData();
      
		int xcoords[4];
		int ycoords[4];
		int zabsolute[4];
		

};

/* Box::Box(int w, int d)
{
            width = w;
            depth = d;
} */

void Box::setData()
{
	
	for (int i=0; i<4; i++)
	{
		cout << "\n\n\tEnter X, Y: " << endl;
		cin >> xcoords[i] >> ycoords[i];
		
		Width = xcoords[i+1] - xcoords[i]; // Width
		Depth = ycoords[i+2] - ycoords[i+1]; // Depth
		L3 = xcoords[i+2] - xcoords[i+3];
		L4 = ycoords[i+3] - ycoords[i];
		int zcoords[] = {5, 5, 5, 3, 4};
		Volume = 0;							// initializing Volume for box;
	}
}
void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << type << right << setw(25) << Volume << endl;
}
void Box::printData()
{
    int Boxnumbers[5];
    cout << fixed;
    cout.precision(2);
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<5;i++)
            {
                for (int j=0;j<4;j++)
                {
                    Boxnumbers[i] = i+1;
                }               
					cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << type << right << setw(25) << Volume << endl;
            }       
    }
}

string Box::getType() 
{
	if (Width == Depth && Depth == Height)
	{
		type = "Square";
	}
	else
	{
		type = "Rectangular";
	}
	return type;
}


int Box::getWidth()
{
	return Width;
}

int Box::getDepth()
{
	return Depth;
}

int Box::getHeight()
{
	const int heightx = 5;
	Height = heightx;
	return Height;
}

int Box::getVolume()
{
	Volume = Width * Depth * Height;
	return Volume;
}


int main()
{
    Box Boxes[5];

	int five[5] = {1, 2, 3, 4, 5};

    for (int i=0; i<5;i++)
    {
		Boxes[i].setData();
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight();
                Boxes[i].getType();
		Boxes[i].getVolume();
                Boxes[i].print1Box();
    }
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
    cout << "\n\n\t" <<endl;
    cout << system("pause")<<endl;
    return EXIT_SUCCESS;
}




        Enter X, Y:
5 5


        Enter X, Y:
5 5


        Enter X, Y:
5 5


        Enter X, Y:
5 5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                      0


        Enter X, Y:



Is it because my data members are not properly initialized? How should I initialize them properly to get the right numbers? Thank you in advance.
I don't understand what the four X,Y values represent. They could for example be the corners of a flat quadrilateral, not a box. Why are you not instead asking the user to supply height, width and depth?

Onto the code, this seems to do nothing useful. the local variable Boxes[] will be discarded when the function ends.
1
2
3
4
5
6
7
8
9
	Box()
        {
            int Boxes[5];
	    Boxes[0]=0;
            Boxes[1]=1;
            Boxes[2]=2;
            Boxes[3]=3;
            Boxes[4]=4;
        }
@Chervil

Thanks for your response. Correcting it now.

The 4 X and Y values represent coordinates (4 points) that collectively make up each box. From those coordinates I should determine the width and depth of the box. Each box, however, has only ONE constant height. This is the data should be entered by the user.

Box 1: 0,0 0,5 14,5 14,0 Height: 5
Box 2: 0,5 0,10 5,10 5,5 Height: 5
Box 3: 5,5 5,10 10,10, 10,5 Height: 5
Box 4: 10,7 10,8 13,8 13,7 Height: 3
Box 5: 10,5 10,7 15,7 15,5 Height: 4

Please help if you can I keep further screwing up my project! :(

Ok, I understand, I think.

But there is still the possibility for the user to enter data which is neither square nor rectangular.
For example, this makes a rectangle: 0,0 0,5 14,5 14,0
but this does not: 0,0 1,5 14,3 14,0
I suppose I look for pitfalls when the user may input something unexpected.

I can think of three ways to deal with this:
a) validate the data and print a message if the data is not valid
b) accept the data, and do the calculations for the oddly mis-shapen box
c) input just two coordinates, representing the diagonally opposite corners, for example your data above might look like this:

Box 1: 0,0 14,5 Height: 5
Box 2: 0,5 5,10 Height: 5
Box 3: 5,5 10,10 Height: 5
Box 4: 10,7 13,8 Height: 3
Box 5: 10,5 15,7 Height: 4

Please accept my apologies for distracting you away from the task of writing actual code.
Thanks, I kind of see what you are saying. I think the problem is I am not properly retrieving the data members or somewhere I am messing up a calculation along the way to make my volume 0. Heres my updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

class Box
{
	private:
		int xcoords[4];
		int ycoords[4];
		int zabsolute[4];
        int tempdepth;
		int tempwidth;
		int tempheight;
        int m_width;
        int m_depth;
        int m_height;
        int m_volume;
        int m_minX;
        int m_maxX;
        int m_maxY;
        int m_minY;
        string m_type;
		int m_L1;
		int m_L2;
		int m_L3;
		int m_L4;
    public:
    	Box();	
        int getDepth();
		int getWidth();
		int getHeight();
		int getVolume();
        int getMaxX();
        int getMaxY();
        int getMinX();
        int getMinY();
        string getType();
        void printData();
		void print1Box();
		void setData();
};
Box::Box()
{
	int Boxes[5];
} 
void Box::setData()
{
    int xcoords[4];
    int ycoords[4];
	for (int i=0; i<4; i++)
	{
		cout << "\n\n\tEnter X, Y: " << endl;
		cin >> xcoords[i] >> ycoords[i];
		
		for (int j =0; j<4; j++)
		{
			m_width = xcoords[j+1] - xcoords[j]; // Width
			m_depth = ycoords[j+2] - ycoords[j+1]; // Depth
			m_L3 = xcoords[j+2] - xcoords[j+3];
			m_L4 = ycoords[j+3] - ycoords[j];
		}
	}
}
void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
}
void Box::printData()
{
    int Boxnumbers[5];
    cout << fixed;
    cout.precision(2);
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<4;j++)
            {
                Boxnumbers[i] = i+1;
            }               
				cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
        }       
    }
}
/*
int Box::getMaxX(){
}
int Box::getMaxY(){
}
int Box::getMinX(){
}
int Box::getMinY(){
}
*/
string Box::getType() 
{
	if (m_width == m_depth && m_depth == m_height)
	{
		m_type = "Square";
	}
	else
	{
		m_type = "Rectangular";
	}
	return m_type;
}


int Box::getWidth()
{
	for (int j=0;j<4;j++)
	{
		tempwidth = xcoords[j+1] - xcoords[j];
	}
	return tempwidth;
}


int Box::getDepth()
{
	for (int j=0;j<4;j++)
	{
		tempdepth = ycoords[j+2] - ycoords[j+1];
	}
	return tempdepth;
}

int Box::getHeight()
{
	for(int j=0;j<4;j++)
	{
	 tempheight = m_height;
	}
	
	return tempheight;
}

int Box::getVolume()
{
	m_volume = m_width * m_depth * m_height;
	return m_volume;
}


int main()
{
    Box Boxes[5];
    for (int i=0; i<5;i++)
    {
		Boxes[i].setData();
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight();
        Boxes[i].getType();
		Boxes[i].getVolume();
        Boxes[i].print1Box();
    }
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}
void Box::setData() - here the x,y values are input.
But where is the height value entered?

Also, in the constructor box::box() it would be a good idea to initialise all the box data with zeros (or whatever makes sense), to avoid uninitialised data causing unexpected results.

Thanks so much. Still working hard on it over here haha. I initialized all the private data members in the constructor Box::Box() to 0; does that work? I also updated the height cout << and cin >> to get height from the user. Still my volume remains 0. What am I missing?? lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

class Box
{
	private:
        int m_volume;
		int xcoords[4];
		int ycoords[4];
		int zabsolute[4];
        int m_width;
        int m_depth;
        int m_height;
        int m_minX;
        int m_maxX;
        int m_maxY;
        int m_minY;
        string m_type;
		int m_L1;
		int m_L2;
		int m_L3;
		int m_L4;
    public:
    	Box();
        int n_volume;	
        int getDepth();
		int getWidth();
		int getHeight(int heightstore);
		int getVolume();
        int getMaxX();
        int getMaxY();
        int getMinX();
        int getMinY();
        string getType();
        void printData();
		void print1Box();
		void setData(int heightstore);
};
Box::Box()
{
	int Boxes[5]; 
	    m_volume = 0;
		xcoords[4] = 0;
		ycoords[4] = 0;
		zabsolute[4] = 0;
        m_width = 0;
        m_depth = 0;
        m_height =0;
        m_minX=0;
        m_maxX=0;
        m_maxY=0;
        m_minY=0;
        string m_type = "";
		int m_L1=0;
		int m_L2=0;
		int m_L3=0;
		int m_L4=0;
		
	
} 
void Box::setData(int heightstore)
{  
	for (int j=0; j<4; j++)
	{
        cout << "\n\n\tEnter X, Y: " << endl;
		cin >> xcoords[4] >> ycoords[4];
		cout << "\n\n\tEnter Height: " << endl;
		cin >> heightstore;
		m_height = heightstore;
		m_width = xcoords[j+1] - xcoords[j]; // Width
		m_depth = ycoords[j+2] - ycoords[j+1]; // Depth
    }
}
void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
}
void Box::printData()
{
    int Boxnumbers[5];
    cout << fixed;
    cout.precision(2);
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<4;j++)
            {
                Boxnumbers[i] = i+1;
            }               
				cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
        }       
    }
}
/*
int Box::getMaxX(){
}
int Box::getMaxY(){
}
int Box::getMinX(){
}
int Box::getMinY(){
}
*/
string Box::getType() 
{
	if (m_width == m_depth && m_depth == m_height)
	{
		m_type = "Square";
	}
	else
	{
		m_type = "Rectangular";
	}
	return m_type;
}


int Box::getWidth()
{
	for (int j=0;j<4;j++)
	{
		m_width = xcoords[j+1] - xcoords[j];
	}
	return m_width;
}


int Box::getDepth()
{
	for (int j=0;j<4;j++)
	{
		m_depth = ycoords[j+2] - ycoords[j+1];
	}
	return m_depth;
}

int Box::getHeight(int heightstore)
{
	m_height = heightstore;
	return m_height;
}

int Box::getVolume()
{
    int n_volume;
	n_volume = m_width * m_depth * m_height;
	return n_volume;
}

int main()
{
    Box Boxes[5];
	int z;
	double y;
	z =5;
	y = 37.20;
    for (int i=0; i<5;i++)
    {
		Boxes[i].setData(z);
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight(y);
        Boxes[i].getType();
		Boxes[i].getVolume();
        Boxes[i].print1Box();
    }
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}



        Enter X, Y:
5 5


        Enter Height:
5


        Enter X, Y:
5 5


        Enter Height:
5


        Enter X, Y:
5 5


        Enter Height:
5


        Enter X, Y:
5 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                      0


        Enter X, Y:
Don't have 2 threads on exactly the same topic - it is very annoying.
Here's a modified version of setData

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Box::setData(int heightstore)
{
    for (int j=0; j<4; j++)
    {
        cout << "\n\n\tEnter X, Y: " << endl;
        cin >> xcoords[j] >> ycoords[j];
    }

    cout << "\n\n\tEnter Height: " << endl;
    cin >> heightstore;

    m_height = heightstore;
    m_width = xcoords[2] - xcoords[0]; // Width
    m_depth = ycoords[1] - ycoords[0]; // Depth
    m_volume = getVolume();

}


There was some code repeated 4 times in the for loop which really belongs outside. I notice there is a parameter heightstore passed as an input when this function is called, however the value will be replaced by the user input.

After getting the four x,y coordinates, the height and width are calculated. Note, there are four possible ways of doing this. Just using the X values here:
width = topright - topleft
or
width = bottomright - bottomleft
or
width = topright - bottomleft
or
width = bottomright - topleft

and similarly for the depth.

I selected one on an arbitrary basis. Note, if the coordinates form a square or rectangle, all four methods will yield an identical result. But it's clear there is some data redundancy here, which is why I suggested earlier defining the rectangle using just two diagonally opposite corners.

Next I stored the volume in the variable m_volume.

Previously the volume was calculated but its value was not stored (nor was it output). That is the main cause of the original problem.

Looking at the program as a whole, there seems to be a certain amount of repetition of code placed within a loop where it should not be. I've a feeling that this code could probably be pruned and simplified quite a lot.
Last edited on
@TheIdeasMan
I tried to learn about vectors but I am time limited at the moment so the knowledge is tricky to apply. I feel more comfortable sticking to arrays for now but I did read your post in detail and appreciate your tips & critiques. I apologize about the double posting.

@Chervil
You da man! Thanks so much! I will continue to work on my program but it still has a few errors. I wrote a huge long note here earlier but i x'ed it out on accident. Have to get some rest so I can continue my project tomorrow morning! I'll update the code here tomorrow with my progress! Thanks again for bearing with me to understand the problem and then helping me to solve it. If you see this post and have some time please know your tips have helped me so much! :DD

Here's my code as it stands please take a look at it if you get a chance! I will be back on tomorrow afternoon with my own updates. As you can see, my output is still a bit messy but I've cleaned the code up alot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

class BoxCollection
{
private:
		int Boxes[5];
public:
		BoxCollection(){}
		int getMaxX();
		int getMaxY();
		int getMinX();
		int getMinY();
};

class Box
{
	private:
        int m_volume;
		int xcoords[4];
		int ycoords[4];

        int m_width;
        int m_depth;
        int m_height;
        string m_type;
		int m_sumvolume;
    public:
    	Box();
        int n_volume;	
        int getDepth();
		int getWidth();
		int getHeight(int heightstore);
		int getVolume();
        string getType();
		void print1Box();
		void setData(int heightstore);
		void printData();
};
Box::Box()
{
	m_volume = 0;
	xcoords[4] = 0;
	ycoords[4] = 0;
    m_width = 0;
    m_depth = 0;
    m_height =0;
    string m_type = "";
	int m_sumvolume = 0;
} 
void Box::setData(int heightstore)
{
    for (int j=0; j<4; j++)
    {
        cout << "\n\n\tEnter X, Y: " << endl;
        cin >> xcoords[j] >> ycoords[j];
    }

    cout << "\n\n\tEnter Height: " << endl;
    cin >> heightstore;

    m_height = heightstore;
    m_width = xcoords[2] - xcoords[0]; // Width
    m_depth = ycoords[1] - ycoords[0]; // Depth
    m_volume = getVolume();
}
void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
}
void Box::printData()
{
    int Boxnumbers[5];
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<4;j++)
            {
                Boxnumbers[i] = i+1;
            }               
				cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
        }       
    }
}
/*
int Box::getMaxX(){
}
int Box::getMaxY(){
}
int Box::getMinX(){
}
int Box::getMinY(){
}
*/
string Box::getType() 
{
	if (m_width == m_depth && m_depth == m_height)
	{
		m_type = "Square";
	}
	else
	{
		m_type = "Rectangular";
	}
	return m_type;
}

int Box::getWidth()
{
	return m_width;
}

int Box::getDepth()
{
	return m_depth;
}

int Box::getHeight(int heightstore)
{
	return m_height;
}

int Box::getVolume()
{
	m_volume = m_width * m_depth * m_height;
	return m_volume;
}

int main()
{
    Box Boxes[5];
    int heightstore;
	heightstore = 0;

	int z;
	int r;
	z =5;
	r = 37;

    for (int i=0; i<5;i++)
    {
		Boxes[i].setData(heightstore);
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight(z);
        Boxes[i].getType();
		Boxes[i].getVolume();
        Boxes[i].print1Box();
    }
	for (int i=0; i<1;i++)
	{
		Boxes[i].printData();
	}
		
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
	cout << "\n\n\t" << endl;
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}




        Enter X, Y:
0 0


        Enter X, Y:
0 5


        Enter X, Y:
14 5


        Enter X, Y:
14 0


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                    350


        Enter X, Y:
0 5


        Enter X, Y:
0 10


        Enter X, Y:
5 10


        Enter X, Y:
5 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Square                                         125


        Enter X, Y:
0 5


        Enter X, Y:
0 10


        Enter X, Y:
5 10


        Enter X, Y:
5 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Square                                         125


        Enter X, Y:
5 5


        Enter X, Y:
5 10


        Enter X, Y:
10 10


        Enter X, Y:
10 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Square                                         125


        Enter X, Y:
10 7


        Enter X, Y:
10 8


        Enter X, Y:
13 8


        Enter X, Y:
13 7


        Enter Height:
3
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                      9
Box#                     Box Type                          Volume(cu/units)
1                        Rectangular                                    350
2                        Rectangular                                    350
3                        Rectangular                                    350
4                        Rectangular                                    350
5                        Rectangular                                    350



Press the enter key to continue ...Press any key to continue . . .

Last edited on
updated code! and sorry about the double post ill try not to again, and thanks to everyone who has taken the time to read it and maybe even comment!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

class Box
{
	private:
        int m_volume;
        int m_width;
        int m_depth;
        int m_height;
        string m_type;
		int m_sumvolume;
		int local_maxx;
		int local_maxy;
		int local_minx;
		int local_miny;
    public:
    	Box();
        int getDepth();
		int xcoords[4];
		int ycoords[4];
		int getWidth();
		int getHeight(int heightstore);
		int getVolume();
        string getType();
		void print1Box();
		void setData(int heightstore);
		void printData();
		void setMoreData();
};
Box::Box()
{
	m_volume = 0;
	xcoords[4] = 0;
	ycoords[4] = 0;
    local_maxx = 0;
	local_maxy = 0;
	local_miny = 0;
	local_minx = 0;
	m_width = 0;
    m_depth = 0;
    m_height =0;
    string m_type = "";
	int m_sumvolume = 0;
} 
void Box::setData(int heightstore)
{
    for (int j=0; j<4; j++)
    {
        cout << "\n\n\tEnter X, Y: " << endl;
        cin >> xcoords[j] >> ycoords[j];
    }

    cout << "\n\n\tEnter Height: " << endl;
    cin >> heightstore;

    m_height = heightstore;
    m_width = xcoords[2] - xcoords[0]; // Width
    m_depth = ycoords[1] - ycoords[0]; // Depth
    m_volume = getVolume();
}
void Box::setMoreData() //track local max and min values for X and Y
{
	for (int j=0;j<4;j++) // 4 x Coords
	{
		if (xcoords[j] > xcoords[j+1])
		{
			local_maxx = xcoords[j];
			xcoords[j] = xcoords[j+1];
			xcoords[j+1] = local_maxx;
		}
		if (ycoords[j] > ycoords[j+1])
		{
			local_maxy = ycoords[j];
			ycoords[j+1]= ycoords[j+1];
			ycoords[j+1] = local_maxy;
		}
		if (xcoords[j] < xcoords[j+1])
		{
			local_minx = xcoords[j];
			xcoords[j] = xcoords[j+1];
			xcoords[j+1] = local_minx;
		}
		if (ycoords[j] < ycoords[j+1])
		{
			local_miny = ycoords[j];
			ycoords[j+1]= ycoords[j+1];
			ycoords[j+1] = local_miny;
		}
	}
}

void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
}
void Box::printData()
{
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<4;j++)
            {
				cout << left << setw(25) << i << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
            }               
        }       
    }
}

string Box::getType() 
{
	if (m_width == m_depth && m_depth == m_height)
	{
		m_type = "Square";
	}
	else
	{
		m_type = "Rectangular";
	}
	return m_type;
}

int Box::getWidth()
{
	return m_width;
}

int Box::getDepth()
{
	return m_depth;
}

int Box::getHeight(int heightstore)
{
	return m_height;
}

int Box::getVolume()
{
	m_volume = m_width * m_depth * m_height;
	return m_volume;
}

class BoxCollection
{
	private:
		int Boxes[5];
		int maxX;
		int maxY;
	public:
		BoxCollection();
		void getMaxX(int Boxes[5], int xcoords[5], int ycoords[5]);
};

void BoxCollection::getMaxX(int Boxes[5], int xcoords[5], int ycoords[5])
{
	int temp;
	for (int i=0; i<5; i++)
	{
		for (int j =0; j<4; j++)
		{
			if (xcoords[j]>xcoords[j+1])
			{
				temp = xcoords[j];
				xcoords[j]=xcoords[j+1];
				xcoords[j+1]=temp;
			}
		}
	}
};

int main()
{
    int heightstore;
	int z;
	int r;
	z =5;
	r = 37;
	heightstore = 0;
	Box Boxes[5]; //declaring 5 objects of type box in "Boxes" array
    for (int i=0; i<5;i++)
    {
		Boxes[i].setData(heightstore);
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight(z);
        Boxes[i].getType();
		Boxes[i].getVolume();
        Boxes[i].print1Box();
		Boxes[i].setMoreData();
    }
	for (int i=0; i<1;i++)
	{
		cout << "\n\n\t" <<endl;
		Boxes[i].printData();
	}
/*	BoxCollection Objects[5];
	for (int i=0; i<5; i++)
	{
		Objects[i].getMaxX();
		Objects[i].getMaxY();
		Objects[i].getMinX();
		Objects[i].getMinY();
	}
*/		
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
	cout << "\n\n\t" << endl;
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

/*

Results go here!

*/

/* vector<int> myBoxVector(3,0); // { 0, 0, 0 }
	vector<int> my2ndboxVector(4,0); // { 0, 0, 0, 0 }
	myBoxVector.push_back(10); // { 0, 0, 0, 0, 10 }
	myBoxVector.pop_back(); 
		for (int i=0; i<5; i++)
		{
			myBoxVector[i] = i; // {0, 1, 2, 3, 4}
		}
		cout << "third element: " << myBoxVector[3] << endl;
		cout << "last element: " << myBoxVector.back() << endl;
		myBoxVector.erase(myBoxVector.begin() +2, myBoxVector.begin() +4);
		for (int i=0; i<32; i++)
		{
			newBoxVector.push_back(i);
			cout << "#" << i << " size: " << newBoxVector.size();
			cout <<", capacity: " << newBoxVector.capacity() << endl;
		}

		cout << "----" << endl;
		vector <int> newVector; //{0,1,2}
		*/
I'm having trouble comparing the max X's between the 5 boxes. Any input would help.
code and output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;
//-----------------------------------------------------------------------------------------------------------------------------
class Box
{
	private:
        int m_volume;
        int m_width;
        int m_depth;
        int m_height;
        string m_type;
		int m_sumvolume;
		int local_maxx;
		int local_maxy;
		int local_minx;
		int local_miny;
    public:
    	Box();
        int getDepth();
		int getWidth();
		int getHeight(int heightstore);
		int getVolume();
		int getMaxX();
		int getMaxY();
		int getMinY();
		int getMinX();
        string getType();
		void print1Box();
		void setData(int heightstore);
		void printData();
		void setMoreData();
		int xcoords[4];
		int ycoords[4];
};
//-----------------------------------------------------------------------------------------------------------------------------
Box::Box()
{
	m_volume = 0;
	xcoords[4] = 0;
	ycoords[4] = 0;
    local_maxx = 0;
	local_maxy = 0;
	local_miny = 0;
	local_minx = 0;
	m_width = 0;
    m_depth = 0;
    m_height =0;
    string m_type = "";
	int m_sumvolume = 0;
} 
void Box::setData(int heightstore)
{
    for (int j=0; j<4; j++)
    {
        cout << "\n\n\tEnter X, Y: " << endl;
        cin >> xcoords[j] >> ycoords[j];
    }

    cout << "\n\n\tEnter Height: " << endl;
    cin >> heightstore;

    m_height = heightstore;
    m_width = xcoords[2] - xcoords[0]; // Width
    m_depth = ycoords[1] - ycoords[0]; // Depth
    m_volume = getVolume();
}
void Box::setMoreData() //track local max and min values for X and Y
{
	for (int j=0;j<4;j++) // 4 x Coords
	{
		if (xcoords[j] > xcoords[j+1])
		{
			local_maxx = xcoords[j];
			xcoords[j] = xcoords[j+1];
			xcoords[j+1] = local_maxx;
		}
		if (ycoords[j] > ycoords[j+1])
		{
			local_maxy = ycoords[j];
			ycoords[j+1]= ycoords[j+1];
			ycoords[j+1] = local_maxy;
		}
		if (xcoords[j] < xcoords[j+1])
		{
			local_minx = xcoords[j];
			xcoords[j] = xcoords[j+1];
			xcoords[j+1] = local_minx;
		}
		if (ycoords[j] < ycoords[j+1])
		{
			local_miny = ycoords[j];
			ycoords[j+1]= ycoords[j+1];
			ycoords[j+1] = local_miny;
		}
	}
}

void Box::print1Box()
{
	cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
	cout << left << setw(25) << "No. 1" << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
}

void Box::printData()
{
	int Boxnumbers[5];
	Boxnumbers[5] = 0;
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<4;i++)
        {
			Boxnumbers[i] = Boxnumbers[i+1];
			cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
        }       
    }
}

string Box::getType() 
{
	if (m_width == m_depth && m_depth == m_height)
	{
		m_type = "Square";
	}
	else
	{
		m_type = "Rectangular";
	}
	return m_type;
}

int Box::getWidth()
{
	return m_width;
}

int Box::getDepth()
{
	return m_depth;
}

int Box::getHeight(int heightstore)
{
	return m_height;
}

int Box::getVolume()
{
	m_volume = m_width * m_depth * m_height;
	return m_volume;
}

int Box::getMaxX()
{
	return local_maxx;
}
int Box::getMaxY()
{
	return local_maxy;
}
int Box::getMinX()
{
	return local_minx;
}
int Box::getMinY()
{
	return local_miny;
}
//-----------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------
class BoxCollection
{
	private:
		int Boxes[5];
		int absmaxX;
		int absmaxY;
		int absminX;
		int absminY;

	public:
		BoxCollection();
		friend int Box::getWidth();
		friend int Box::getHeight(int heightstore);
		friend int Box::getDepth();

		friend void Box::setMoreData();
		friend int Box::getMaxX();
		friend int Box::getMaxY();
		friend int Box::getMinX();
		friend int Box::getMinY();
		int getabsmaxX();
		int getabsminX();
		int getabsmaxY();
		int getabsminY();
		void RetrieveData();
};

void BoxCollection::RetrieveData()
{
	for (int i=0;i<5;i++)
	{
		if (i>i+1)
		{
			i = absmaxX;
		}
	}
};
//-----------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------------
int main()
{
    int heightstore;
	int z;
	int r;
	z =5;
	r = 37;
	heightstore = 0;
	Box Boxes[5]; //declaring 5 objects of type box in "Boxes" array
    
	for (int i=0; i<5;i++)
    {
		Boxes[i].setData(heightstore);
		Boxes[i].getWidth();
		Boxes[i].getDepth();
		Boxes[i].getHeight(z);
        Boxes[i].getType();
		Boxes[i].getVolume();
        Boxes[i].print1Box();
		Boxes[i].setMoreData();
		Boxes[i].getMaxX();
		Boxes[i].getMaxY();
		Boxes[i].getMinX();
		Boxes[i].getMinY();
    }
	
	for (int i=0; i<1;i++)
	{
		cout << "\n\n\t" <<endl;
		Boxes[i].printData();
	}
/*	
	BoxCollection Objects[5];
	
	for (int i=0; i<5; i++)
	{
		Objects[i].getabsmaxX();
		Objects[i].getabsmaxY();
		Objects[i].getabsminX();
		Objects[i].getabsminY();
	}
*/
    //declare an array of 5 objects of type Box

    //call a function to load the user input into each object

    //call a function to print results for part #1 and #2

    //call a function to print results for part #3

    //print the results for part #4
	cout << "\n\n\t" << endl;
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------------------------------------------------------



        Enter X, Y:
0 0


        Enter X, Y:
0 5


        Enter X, Y:
14 5


        Enter X, Y:
14 0


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                    350


        Enter X, Y:
0 5


        Enter X, Y:
0 10


        Enter X, Y:
5 10


        Enter X, Y:
5 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Square                                         125


        Enter X, Y:
5 5


        Enter X, Y:
5 10


        Enter X, Y:
10 10


        Enter X, Y:
10 5


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
No. 1                    Square                                         125


        Enter X, Y:
10 7


        Enter X, Y:
10 8


        Enter X, Y:
13 8


        Enter X, Y:
13 7


        Enter Height:
3
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                      9


        Enter X, Y:
10 5


        Enter X, Y:
10 7


        Enter X, Y:
15 7


        Enter X, Y:
15 5


        Enter Height:
4
Box#                     Box Type                          Volume(cu/units)
No. 1                    Rectangular                                     40



Box#                     Box Type                          Volume(cu/units)
-858993460               Rectangular                                    350
-858993460               Rectangular                                    350
-858993460               Rectangular                                    350
-858993460               Rectangular                                    350
so compare like below? still using xcoords[4]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int Box::getMaxX()
{
	if(xcoords[2]>xcoords[0])
	{
		local_maxx = xcoords[2];
	}
	return local_maxx;
}
int Box::getMaxY()
{		
	if(ycoords[2]>ycoords[0])
	{
		local_maxy = ycoords[2];
	}
	return local_maxy;
}
int Box::getMinX()
{		
	if(xcoords[0]<xcoords[2])
	{
		local_minx = xcoords[2];
	}	
	return local_minx;
}
int Box::getMinY()
{
	if(ycoords[0]<ycoords[2])
	{
		local_minx = xcoords[2];
	}	
	return local_miny;
}

?
still cant get print function to print properly. how can i access the different volumes for the different boxes if i havent specified a name or a way to identify them to add up their volumes? I feel like I shouldn't need to store them in an in an array of volumes for each individual object?


        Enter X, Y:
0 0


        Enter X, Y:
0 5


        Enter X, Y:
14 5


        Enter X, Y:
14 0


        Enter Height:
5
Box#                     Box Type                          Volume(cu/units)
1                        Rectangular                                    350
Box#                     Box Type                          Volume(cu/units)
1                        Rectangular                                    350
2                        Rectangular                                    350
3                        Rectangular                                    350
4                        Rectangular                                    350
5                        Rectangular                                    350


        Enter X, Y:
Last edited on
obviously i shouldnt run that 2nd print function in the same loop but i dont know how to access the different volumes and different types after calculating them, i dont know should i store an array of string boxtypes[5] with 5 types specifying rectangular or square for each box? Same for volume?
One thing I don't understand. Is this just an idea of your own that you came up with, or is there a specification or description of the requirements anywhere?

I ask because it still doesn't make a lot of sense to me.
@Chervil no this is a college homework assignment due tonight that I've been working on for the past 2 weeks but haven't been able to make work. I can provide the specs if u are imterested, and I appreciate your continued support. I believe the professor made it awkward (ie forced to declare an array of box objects) simply to avoid online searches for the material. I have correctly input the data, I have checked with print1box the the result which is also accurate for each rspective box.the problem is I should call a function to print the data of the 5 box's, then the total volume of the 5 boxes. I don't know how to do this, and can only manage to get the print function to return the first objects attributes 5 times incorrectly instead of tracking each specific box for no, type and volume. I can't get it to display the 3 columns of 5 values each properly. Please let me know if you'd like to see the specs? I will provide as soon as I can get to computer. Thanks again.

1
2
3
4
5
6
7
8
9
10
11
12
13
void Box::printData()
{
	int Boxnumbers[5];
	Boxnumbers[5] = 0;
    cout << left << setw(25) << "Box#" << left << setw(25) << "Box Type" << right << setw(25) << "Volume(cu/units)" << endl;
    {
        for (int i=0;i<4;i++)
        {
			Boxnumbers[i] = Boxnumbers[i+1];
			cout << left << setw(25) << Boxnumbers[i] << left << setw(25) << m_type << right << setw(25) << m_volume << endl;
        }       
    }
}


I'm not sure what you're trying to accomplish here. First you declare an array of 5 numbers, then you set the 6th (non-existent) element of that array to 0, clobbering memory you don't own. At this point, the entire array contains whatever random junk was there before Boxnumbers existed. Then you proceed to use it as if Boxnumbers didn't contain junk.

Try to remember that a Box object is just one box, so a Box::Printdata() function, which has access to only the Box object for which it is invoked, can only print out data for one box.
Last edited on
Pages: 12