EmbraceNext
EmbraceNext

Reputation: 75

Intro to programming project. Stuck on creating a data file from a table

I have been given a project for class and am stuck on a part of it. The project has this table in it:

enter image description here

How can I create this information into a sequential file to read into a C++ program? It looks like this would be a 3 dimensional array. Am I looking too far into this?

Upvotes: 3

Views: 180

Answers (3)

FernandoZ
FernandoZ

Reputation: 438

It depends on what you are trying to do. Typically if you are are just trying to read it in you can store it as a flat file. With flat files you typically have two common formats fixed length record or comma separate (there are other flat file formats you can implement but these are two common fomats).

In fixed length record each record is a fixed length and each field is a fixed length. For example

Turkey             1.00Swiss        .30Roll           .30Condiments         .10
Pastrami           1.30Swiss        .40Roll           .30Condiments         .10

In comma separated language you can do it like this Turkey,1.00,Swiss,.30,Roll,.30,Condiment,.10

With fixed length record the con is that once specified and depending on who is the recipient of the file is your fields can never grow. For example if your price field is five spaces you max value ever would be 99.99 and you would have a hard time ever increasing the field size because it would no longer be compatible if some other application depende on it being 5 characters. Comma separated is a little more flexible because the separator is the comma. With comma separate you can usually have the fields grow in size because your comma is the separator.

There is another option and that is to create a ini type of what I will call Domain Specific format as an example take a look below

Sandwich: Turkey
Deli Slice: Turkey,1.00
Deli Slice: Swiss,.30
Bread: Roll,.30
Condiments: .30

Sandwich: Pastrami
Deli Slice: Pastrami,1.00
Deli Slice: Swiss,.40
Bread: Roll,.30
Condiments: .10

This format is more flexible because you can add more fields. For example lets say you have a new sandwich called a california and it required meat cheese and also avocado. You can extend it like this

Sandwich: California
Deli Slice: Turkey, 1.00
Deli Slice: American, .30
Deli Slice: Avocado, .50
Bread: Roll,.30
Condiments: .10

With this third format and some careful programming your new sandwich will automatically be backwards compatible.

Each of the files takes a different strategy for reading it in. There is something called parsing and each file would have a different approach. In the fixed length record the approach wouldnbe to read each column by character positions. In the comma separated file you would need to tokenize each line read in in C++ you have a strtok but if you use managed C++ .net i believe you have a string.split. With the third option you actually look for keywoards such as "Sandwich", "Deli SLice" etc and then you also have to tokenize the itmes because they

Upvotes: 1

noMAD
noMAD

Reputation: 7844

I am not sure if you know about HashMaps. If not, you could look it up. It would be very simple using that.

You can have one map having key = String and value = Integer. There will be 4 keys, "Turkey", "pastrami", "Ham" and "Special" and you can have values associated with them as 1,2,3,4.

Then you can have 4 other maps each having key = String and Value = float and enter the above data. You can use the 1st map to check for the proper column and the other maps to get the cost.

You can of-course implement the above using 2D - arrays but it would be a good experience to learn. Good Luck!!

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

There are all kinds of conventions you might use. For example,

Turkey
Turkey, 1.00
Swiss,  0.30
Roll,   0.30
Condiments, 0.10

Pastrami
Pastrami, 1.30
Swiss,     .40
...

This format would be easy enough to read: table header on a line, then ingredient/price pairs separated by a comma, then a blank line. Then it starts over again.

Upvotes: 0

Related Questions