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.
Technology Partners: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 71
C++ get user from AD  (VS6) 7 108
Need some help with Microsoft Visual Studio C++ 2003 5 85
Need some help with mailto 16 40
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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