### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290`` ``````#include #include #include #include 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 "<> 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]> 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]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" <

 ``` 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.

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,

 ``1234567891011121314`` ``````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:

 ``1234`` ``````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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300`` ``````//----------------------------------------------------------------------------// // Name: // // Student ID: // // Assignment: #2 // //----------------------------------------------------------------------------// #include #include #include #include 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 "<> 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]> 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]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" <

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.
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.
 ``12345`` ``````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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177`` `````` #include #include #include #include #include #include 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:

 ``12`` ``````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:
 ``1234`` ``````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.

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:

 ``12345678`` ``````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...
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252`` ``````#include #include #include #include #include #include #include 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" < myBoxVector(3,0); // { 0, 0, 0 } vector 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 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.