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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 77
getting a fire fox page (view page source ) in c++ 6 45
thread-safe code in c++ 2 120
Precision Problem in C++ 7 48
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 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