Solved

implementiing a table with headers, rows, and columns.

Posted on 2004-03-25
6
232 Views
Last Modified: 2010-04-01
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
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

//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
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.

Thanks

Kaz
0
Comment
Question by:KazIT
6 Comments
 
LVL 44

Accepted Solution

by:
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
2:Bearings:25.50:20:10
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.
0
 
LVL 30

Expert Comment

by:Axter
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.
0
 
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Assisted Solution

by:Sys_Prog
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

Amit
 


0
 
LVL 3

Assisted Solution

by:idt
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.
0
 

Author Comment

by:KazIT
ID: 10722436
Thank you all for the help.

KazIT
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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. …
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.

860 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