@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 !!