[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 264

# implementiing a table with headers, rows, and columns.

Hi

I have to write a source code  for a spare parts company.  There are only 3 "Parts" that i need to specify
Code    Part   Price   Quantity On Hand   Reorder Level
I have inital values available.
What I am trying to work out is:
Is there some way to initialise the Parts with the values without having to define and initialise 3 times.
eg: having to repeat the variables for each part
source code i have so far is

//DEFINE AND INITIALISE PART 1 OBJECTS AND VARIABLES
int Code1 = 1;            //CODE PART 1
string Part1 = "Rear Axle";      //PART REAR AXLE
double Price1 = 128.00;      //PRICE PART 1
int QuantityOnHand1 = 5;      //QUANTITY ON HAND PART 1
int reorderLevel1 = 2;            //REORDER LEVEL PART 1

//DEFINE AND INITIALISE PART 2 OBJECTS AND VARIABLES
int Code2 = 2;            //CODE PART 2
string Part2 = "Bearings";      //PART BEARINGS
double Price2 = 25.50;      //PRICE PART 2
int QuantityOnHand2 = 20;      //QUANTITY ON HAND PART 2
int reorderLevel2 = 10;      //REORDER LEVEL PART 2

//DEFINE AND INITIALISE PART 3 OBJECTS AND VARIABLES
int Code3 = 3;            //CODE PART 3
double Price3 = 30.00;      //PRICE PART 3
int QuantityOnHand3 = 12;      //QUANTITY ON HAND PART 3
int reorderLevel3 = 6;            //REORDER LEVEL PART 3

and can you tell me how I make this information appear as a table?

This is my first go at a code that is like this.

Thanks

Kaz
0
KazIT
3 Solutions

Commented:
I assume this is homework, so I won't give you the full solution

To initialize your data you have a number of options:

1:Rear Axle:128.00;5:2
2:Bearings:25.50:20:10

Or, you can write a function that initializes one record at a time:

PartDataRecord InitializeRecord(int code, string name, price double, int quantitiyOnHand);

You should not hardcode the max. number of parts your program can process (e.g. by using Code1, Code2, Code3). Use a container to store these parts. One option is to use a STL container (e.g. a vector or a list, or even a map<int, PartDataRecord> to map the part code to the actual data), or you can write your own container, or just use an array. All these options however require that you define a data structure to hold your part information. I called this PartDataRecord. This will look something like this:

struct PartDataRecord{
int m_code;
string m_name;
double m_price;
int quantityOnHand;
};

It would however be better to create a class for that, so that you can take care of initializing the record correctly. You could use a constructor to assign the values that you read from your parts configuration file.
0

Commented:
It looks like you should be using an array.
Try defineing a class that has these variables, and then using a vector to store each record.
0

Commented:
To write the data out in table form, you could use the ostream::width() method, or the  fprintf function that allows you to specify field widths as part of the % format string.
This way, all items from the same column will have the same width. Lookup the information about the cout or fprintf syntax and ask if you need more information.
0

Commented:
>>Is there some way to initialise the Parts with the values without having to define and initialise 3 times.

No there isn't any way to skip definning 3 times
Also, there isn't any way to initialise all the "Parts" without a separate initialization for each of them

But yes, there is a way to save some typing effort and making your code more managable

This can be done by representing "Part" as a class with each of the attributes (e.g. Code, Part-desc, Price) as private data members of this class
This class can have a constructor which would take a parameter for each of the attributes
All the parameters in the Constructor could have default values
You can use the constructor initializer list to initialse each of your class data members
You could have a member function "display_info()" in the class. This member function would print each attribute(data member) of the class

Having done that, You could now just create three objects of class Part (say in main() ) with the values you have and just use the display_info() for each object to display the Parts

Amit

0

Commented:
Just right out of left field... And you'll get extra marks in class if you show up with this one..

I suggest that when writing applications that involve currency, that you store the smallest resoltion of the currency as an integer.

That means for dollars, store only the cents. is do not store 1.99 , store 199.

In base 2, 1/1010 (that is, 1/10 decimal) is an infinitely-repeating fraction: its binary representation is 0.0001100110011... . Depending on how carefully your compiler's binary/decimal conversion routines (such a s those used by printf) have been written, you may see discrepancies when numbers (especially low-precision floats) not exactly representable in base 2 are a ssigned or read in and then printed (i.e. converted from base 10 to base 2 and back again).

So.. if you store in cents, then all of your calculations are whole integer calculations, no rounding problems during such calculations, no irate customers demanding to know why your application of taxes differs from theirs by \$0.01, no one in accounting will be asking you when you will be fixing the 'bug' etc..

After all calculation to the cents are complete, final reports can report as cents divided by 100 to show \$x.yy

Daniel P.
0

Author Commented:
Thank you all for the help.

KazIT
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.