Output & Computation Errors

Hey Everyone,

So I'm trying to get the width, depth, and height of 5 boxes, and then using these dimensions to determine each box's volume. Any help would be kindly appreciated.
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Box
{
    public:
    int x[5];
    int y[5];
	int z[5];
        Box::Box()
        {
            int Boxes[5];
			Boxes[0]=0;
            Boxes[1]=1;
            Boxes[2]=2;
            Boxes[3]=3;
            Boxes[4]=4;
			int *xpointer;
			xpointer = &Boxes[0];
        }

        Box(int x[5], int y[5], int z[5])
        {
			for (int i = 0; i<5;i++)
			{
				width = x[i];
				depth = y[i];
				height = z[i];
			}
           
        }
        Box(int w, int d, int h, string t, int v)
        {
            width = w;
            depth = d;
            height = h;
            type = t;
            Volume = v;
        }
        Box(int w, int d, int h, string t, int v, int mix, int max, int miy, int may)
        {
            width = w;
            depth = d;
            height = h;
            type = t;
            Volume = v;
            maxX = max;
            minX = mix;
            maxY = may;
            minY = miy;
        }
        void Box::setData()
        {
			for (int i=0; i<5; i++)
			{
					cout << "\n\n\tEnter X, Y, & Height: " << endl;
					cin >> x[i] >> y[i] >> z[i];
					width = x[i];
					depth = y[i];
					height = z[i];
			}
        }
        int getDepth();
        int getMaxX(int x[20], int y[20]);
        int getMaxY(int x[20], int y[20]);
        int getMinX(int x[20], int y[20]);
        int getMinY(int x[20], int y[20]);
        string getType();
        int getVolume();
        int getWidth();
		int getHeight();
        void Box::printData();

    private:
        int width;
        int depth;
        int height;
        int Volume;
        int minX;
        int maxX;
        int maxY;
        int minY;
        string type;
        int Boxes[5];
};

/* Box::Box(int w, int d)
{
            width = w;
            depth = d;
} */
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;
            }
    }
}
int Box::getDepth()
{
    for (int i=0; i<5; i++)
    {
          depth = y[i];
    }
		  return depth;
}

/* int Box::getMaxX(int earray[24][24]) // Highest Value of X from the 4 X and Y pairs
{
    for (int i=0;i<24;i++)
    {
        for (int j = 0; j<23;j++)
        {
            temp = maxX[j];
            maxX[j]=maxX[j+1];
            maxX[j+1] = temp;
        }
    }
    return maxX;
} */
/*
int Box::getMaxY(int x[20], int y[20]) // Highest Value of Y from the 4 X and Y pairs
{
    for (int x=0;x<24;x++)
    {
        for (int y = 0; y<23;y++)
        {
            cout<<"insert the value "<<y+1<<endl;
            cin>> earray[x][y];
        }
        int maxY;
        int minY;
        maxY = earray[24][0];
        minY = earray[0][24];
        int e = 0;
        while (e<24)
        {
            if (earray[24][0]>maxY)
                {
                    maxY = earray[e][24];
                }
            else if (earray[e][24]<minY)
                {
                    minY = earray[e][24];
                }
            e++;
        }
    }
return maxY;
}

int Box::getMinX(int x[20], int y[20]) // Lowest Value of X from the 4 X and Y pairs
{
    for (int x=0;x<24;x++)
    {
        for (int y = 0; y<23;y++)
        {
            cout<<"insert the value "<<x+1<<endl;
            cin>> earray[x][y];
        }
            int minX;
            int maxX;
            int e = 0;
            maxX = earray[e][24];
            minX = earray[0][24];
                while (e<24)
                {
                    if (earray[e][24]<minX)
                        {
                            minX = earray[e][24];
                        }
                        else if (earray[e][24]>maxX)
                        {
                            maxX = earray[e][24];
                        }
                    e++;
                }
    }
return minX;
}

int Box::getMinY(int x[20], int y[20]) //Lowest Value of Y the 4 X and Y pairs
{
    for (int i=0;i<100;i++)
    {
        for (int j=0; j<150; j++)
        {
            while (i == i+1)
            {
                if (j<=j+1)
                {
                    minY = j;
                }
                else
                {
                    minY = j+1;
                }

            }
        }
    }
return minY;
}
*/
string Box::getType() 
{
	for (int i=0; i<5; i++)
	{
			if (width == depth && depth == height)
				{
				type = "Square";
				}
			else
				{
				type = "Rectangular";
				}
	}
		return type;
}

int Box::getVolume()
{
	for (int i=0; i<5; i++)
	{
		Volume = x[i] * y[i] * z[i];
	}
		return Volume;
}

int Box::getWidth()
{
    for (int i=0; i<5; i++)
    {
        width = y[i];
       
    }
		return width;
}
int Box::getHeight()
{
    for (int i=0; i<5; i++)
    {
        height = z[i];
        return height;
    }
		return height;
}

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].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 << system("pause")<<endl;
    return EXIT_SUCCESS;
}




        Enter X, Y, & Height:
5 0 5


        Enter X, Y, & Height:
5 2 2


        Enter X, Y, & Height:
3 1 4


        Enter X, Y, & Height:
6 5 4


        Enter X, Y, & Height:
2 1 3
Box#                     Box Type                          Volume(cu/units)
1                        Rectangular                                      6
2                        Rectangular                                      6
3                        Rectangular                                      6
4                        Rectangular                                      6
5                        Rectangular                                      6

        Enter X, Y, & Height:

Last edited on
Does a box have multiple boxes contained within it?
@ilovelearning

First thing is not to use using namespace std; It brings heaps of stuff into the global namespace - polluting it, which can cause conflicts with other user or library namespaces.

Instead do either of these:

1. Put std:: bfore each std thing that you use.
2. Put using std::cout; after the #include - do similar for other std things such string, vector, endl, cin etc.

So I'm trying to get the width, depth, and height of 5 boxes,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Box
{
    public:
    int x[5];
    int y[5];
	int z[5];
        Box::Box()
        {
            int Boxes[5];
			Boxes[0]=0;
            Boxes[1]=1;
            Boxes[2]=2;
            Boxes[3]=3;
            Boxes[4]=4;


Would it be better to have a Box class that holds info for 1 box, then in main create a <vector> of 5 Box objects. You can use push_front or push_back to put objects into a vector. There is no real need to specify the size of the vector before hand, and the vector can grow.

Currently you have arrays of 5 x,y,z's within the Box class, as well as having an array called Boxes, with 5 elements in it, and get functions that take arguments of an array of 20.

I find it easier to have the private variables declared before the others, so they are declared before they are used, and they appear before the functions - I find it easier to read that way, although it is a personal preference.

The usual convention is to name member variables with a leading m_ so that you know they are member variables, and makes it easier with names of function parameters. I also name classes with a leading C, as in CBox.

Also, with file organisation - it is normal to place the declaration of classes, their member variables and functions, into a header file. The defintion of the class functions go into a .cpp file.

Edit: When I create a class in my IDE, it creates the .h and .cpp files automatically. Also when I create a new member function, it copies it to the .cpp file automatically. I am sure you can do the same thing in your IDE.

With cout - you can build up the output in stages so you don't have a statement that is 150 odd characters long. There is a bit of a school of thought that code shouldn't exceed 80 chars per line. You can set the cout settings before outputting info, and there is no need to repeat setw clauses. Try doing this instead:

1
2
3
4
cout << left << setw(25) ;
cout "Box#" << "Box Type";
cout  << right;
cout  "Volume(cu/units)" << endl;


If you find yourself repeatedly changing the cout settings, then investigate the use of the cout clause setflags, and consider using a function to change the settings.

With your get functions, they contain for loops, that only return the value of the last element in the array, which I think is the main cause of your problems.

Finally, your program will be much more useful, if change the types to double rather than int.

Hope this all heps -- Good Luck !!




Last edited on
@TheIdeasMan im so sorry I don't get it still... How can I get my functions to return width[i] height[i] depth[i]? :( Please help.

@pogrady - Each box has 5 sets of x and y coordinates, but I constructed an array of 5 Boxes as instructed. My pointer there is useless as I am unsure as how to use it to get the values in my boxes. my new code is like this:

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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//----------------------------------------------------------------------------//
// Name:                                                                      //
// Student ID:                                                                //
// Assignment: #2                                                             //
//----------------------------------------------------------------------------//
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Box
{
	 private:
        int width[5];
        int depth[5];
        int height[5];
        int Volume[5];
        int minX;
        int maxX;
        int maxY;
        int minY;
        string type;
        int Boxes[5];
    public:
		int x[5];
		int y[5];
		int z[5];
        Box::Box()
        {
            int Boxes[5];
			Boxes[0]=0;
            Boxes[1]=1;
            Boxes[2]=2;
            Boxes[3]=3;
            Boxes[4]=4;
			int *xpointer;
			xpointer = &Boxes[0];
        }

        Box(int x[5], int y[5], int z[5])
        {
			for (int i = 0; i<5;i++)
			{
				width[5] = x[i];
				depth[5] = y[i];
				height[5] = z[i];
				Volume[5] = Volume[i];
			}
        }
        Box(int w, int d, int h, string t, int v)
        {
            width[5] = w;
            depth[5] = d;
            height[5] = h;
            type = t;
            Volume[5] = v;
        }
        Box(int w, int d, int h, string t, int v, int mix, int max, int miy, int may)
        {
            width[5] = w;
            depth[5] = d;
            height[5] = h;
            type = t;
            Volume[5] = v;
            maxX = max;
            minX = mix;
            maxY = may;
            minY = miy;
        }
        void Box::setData()
        {
			for (int i=0; i<5; i++)
			{

			    cout << "\n\n\tEnter X, Y, & Height: " << endl;
			    cin >> x[i] >> y[i] >> z[i];

			}
        }
        int getDepth();
        int getMaxX();
        int getMaxY();
        int getMinX();
        int getMinY();
        string getType();
        int getVolume();
        int getWidth();
		int getHeight();
        void Box::printData();

   
};

/* Box::Box(int w, int d)
{
            width = w;
            depth = d;
} */
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;
            }       
    }
}
int Box::getDepth()
{
    for (int i=0; i<5; i++)
    {
          depth[i] = y[i];
    }
}

/* int Box::getMaxX(int earray[24][24]) // Highest Value of X from the 4 X and Y pairs
{
    for (int i=0;i<24;i++)
    {
        for (int j = 0; j<23;j++)
        {
            temp = maxX[j];
            maxX[j]=maxX[j+1];
            maxX[j+1] = temp;
        }
    }
    return maxX;
} */
/*
int Box::getMaxY(int x[20], int y[20]) // Highest Value of Y from the 4 X and Y pairs
{
    for (int x=0;x<24;x++)
    {
        for (int y = 0; y<23;y++)
        {
            cout<<"insert the value "<<y+1<<endl;
            cin>> earray[x][y];
        }
        int maxY;
        int minY;
        maxY = earray[24][0];
        minY = earray[0][24];
        int e = 0;
        while (e<24)
        {
            if (earray[24][0]>maxY)
                {
                    maxY = earray[e][24];
                }
            else if (earray[e][24]<minY)
                {
                    minY = earray[e][24];
                }
            e++;
        }
    }
return maxY;
}

int Box::getMinX(int x[20], int y[20]) // Lowest Value of X from the 4 X and Y pairs
{
    for (int x=0;x<24;x++)
    {
        for (int y = 0; y<23;y++)
        {
            cout<<"insert the value "<<x+1<<endl;
            cin>> earray[x][y];
        }
            int minX;
            int maxX;
            int e = 0;
            maxX = earray[e][24];
            minX = earray[0][24];
                while (e<24)
                {
                    if (earray[e][24]<minX)
                        {
                            minX = earray[e][24];
                        }
                        else if (earray[e][24]>maxX)
                        {
                            maxX = earray[e][24];
                        }
                    e++;
                }
    }
return minX;
}

int Box::getMinY(int x[20], int y[20]) //Lowest Value of Y the 4 X and Y pairs
{
    for (int i=0;i<100;i++)
    {
        for (int j=0; j<150; j++)
        {
            while (i == i+1)
            {
                if (j<=j+1)
                {
                    minY = j;
                }
                else
                {
                    minY = j+1;
                }

            }
        }
    }
return minY;
}
*/
string Box::getType() 
{
	for (int i=0; i<5; i++)
	{
			if (width == depth && depth == height)
				{
				type = "Square";
				}
			else
				{
				type = "Rectangular";
				}
	}
		return type;
}

int Box::getVolume()
{
	for (int i=0; i<5; i++)
	{
		Volume[i] = x[i] * y[i] * z[i];
	}
}

int Box::getWidth()
{
    for (int i=0; i<5; i++)
    {
        width[i] = y[i];
	}
}

int Box::getHeight()
{
    for (int i=0; i<5; i++)
    {
        height[i] = z[i] * 1;
    }
}

int main()
{
    Box Boxes[5];
    Box *xpointer;
    xpointer = &Boxes[0];
    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].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 << system("pause")<<endl;
    return EXIT_SUCCESS;
}


/*

Results go here!

*/


i know my getWidth, getHeight, and getDepth functions don't return a value right now but i don't know what I can do to change it from e.g. height[5] to height[i] as the output. Thanks in advance.
Bump any advice on my get functions and set function pleasee
It would be easier if you just make 1 thread...
TheIdeasMan wrote:
Would it be better to have a Box class that holds info for 1 box, then in main create a <vector> of 5 Box objects. (...)
Currently you have arrays of 5 x,y,z's within the Box class, as well as having an array called Boxes, with 5 elements in it, and get functions that take arguments of an array of 20.


OP wrote:
Each box has 5 sets of x and y coordinates
Listen to yourself, that makes no sense.
1
2
3
4
5
class box{
public:
   int width, height, depth;
   int volume() const;
};
im so sorry i dont know how to use vectors yet. I agree that was quite stupid as a box only has 4 vertices...
I hate to spam but I REALLY need to figure out how to fix my program so as many eyes on it as possible is my aim. 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
177

#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 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()
{  
	for (int j=0; j<4; j++)
	{
        cout << "\n\n\tEnter X, Y: " << endl;
		cin >> xcoords[4] >> ycoords[4];
	    if (j+1 != j && j != j+2)
		  {
		      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 tempheight;
	for(int j=0;j<4;j++)
	{
	   tempheight = m_height;
	}
	return tempheight;
}

int Box::getVolume()
{
    int n_volume;
	n_volume = m_width * m_depth * m_height;
	return n_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;
}

/*

Results go here!

*/
You still have an array inside the box class - why do you do this?

This part is confusing:

1
2
int m_depth;
int m_height;


Which is the y or z? They both look like z's. Why not have length, width & height?


With these:
1
2
3
4
int getMaxX();
        int getMaxY();
        int getMinX();
        int getMinY();


are they supposed to operate on the array of boxes? That is find the max X of all the boxes?

As I said earlier - you need a BoxCollection class, which has a vector of box objects in it, as well as functions that do operations on the vector of objects.

I think you should read my previous comments more carefully.

If you don't know how to use a vector - look at the reference page - the top left of this page. Or Google C++ vector example and it will take you there.


@ideasman
Thank you for the suggestion, I will try to implement it asap, can u give me an example of what a constructor may look like for my BoxCollection class? I appreciate it.
This is why it is confusing to have two threads - I saw you BoxCollection class in the other thread.

You would be better to have constructors for the Box class - the arguments are values that you use to set member variables.

I don't see the need to store he 4 corners of the box - you only need 2 as others have said.

You don't seem to understand why this doesn't work:

1
2
3
4
5
6
7
8
int Box::getWidth()
{
	for (int j=0;j<4;j++)
	{
		m_width = xcoords[j+1] - xcoords[j];  //j+1 is out of bounds
	}
	return m_width;  //this is  xcoords[4] - xcoords[3] 
}


This function needs to operate on 1 box, not 4 of them. 4 is out of bounds for this array. The for loop is pointless.
Please take a look at my revised code. I am now trying to track local min and max x and y values, then use the Boxcollection class to sort through the local data of the 5 box objects. I appreciate it...
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
#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;
}


/* 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}
		*/
My problem is that once I get the local max and min X's and Y's for each individual box, I am having trouble comparing then using a different "BoxCollection" class because I can't access the variables I stored for max and Min values from my "Box" class. How do you pass values between classes? Please help. Thanks.
Topic archived. No new replies allowed.