implementiing a table with headers, rows, and columns.

Posted on 2004-03-25
Last Modified: 2010-04-01

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
are the headers
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

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

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

int Code3 = 3;            //CODE PART 3
string Part3 = "Brake Pads";      //PART BRAKE PADS
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.


Question by:KazIT
LVL 44

Accepted Solution

Karl Heinz Kremer earned 225 total points
ID: 10676220
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
3:Brake Pads:30.00:12:6

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.
LVL 30

Expert Comment

ID: 10676244
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.
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10676251
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.
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 10

Assisted Solution

Sys_Prog earned 225 total points
ID: 10676804
>>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



Assisted Solution

idt earned 50 total points
ID: 10676993
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.

Author Comment

ID: 10722436
Thank you all for the help.


Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now