adding one additional field to the Structure of the records

I need help adding one additional field to the Structure of the records in each of these 3 programs:

• That is a char field called ID which should have an ID_SIZE of 5. But I don't know were to add it

• Once the three programs are written and operating, then I need to Enter the following data into each of the five records:
1
2
3
4
5
6
Desc	ID	qty	price
Wrench	WR1	10	$4.67
Hammer	HR2	15	$3.97
Saw	SW3	5	$4.95
Pliers	PL4	7	$5.50
Crowbar	CR5	2	$7.00


here a sample programs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Program 1
// This program sets up a file of blank inventory records.
#include<iostream>
#include<fstream>
using namespace std;
// Constants
const int DESC_SIZE = 31;
 const int NUM_RECORDS = 5;
// Declaration of InventoryItem structure
 struct InventoryItem {
char desc[DESC_SIZE];
int qty;
double price;
};

int main()
{
// Create an empty Inventoryitem structure.
InventoryItem record = { "", 0, 0.0 };
// Open the file for binary output.
fstream inventory("Inventory.dat", ios::out | ios::binary);
// Write the blank records.
for (int count = 0; count < NUM_RECORDS; count++)
{
cout << "Now writing record " << count << endl;
inventory.write(reinterpret_cast<char *>(&record),
                 sizeof(record));
} // Close the file.
inventory.close();
return 0;
}


[
Last edited on
> But I don't know were to add it
You add it in the structure - in all three programs.

To cut down on the editing, put the structure in a header file.
1
2
3
4
5
6
7
8
9
10
// db_struct.h
const int DESC_SIZE = 31;
// Description size
// Declaration of InventoryItem structure
struct InventoryItem
{
    char desc[DESC_SIZE];
    int qty;
    double price;
};


Then in each program, where you have the struct, you just have
#include "db_struct.h"

Where you add it in the structure is entirely up to you. Immediately after desc seems logical and is consistent with your input data order. But your programs wouldn't care at all if you put the ID anywhere else in the structure. You only have to ensure that it's consistent between the programs (see the header file above).

A big question would be
Do you care about backward compatibility for existing "Inventory.dat" files?


Last edited on
Thanks Salem, for your question yes

Also, I'm not allowed to use a header file in this lab. I tried what you said but the data is show vertically, I need to show it horizontally

solved, cheers
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Program 1
// This program sets up a file of blank inventory records.
#include <iostream>
#include <fstream>
using namespace std;

// Constants
const size_t DESC_SIZE {31};
const size_t NUM_RECORDS {5};
const size_t ID_SIZE {5};

// Declaration of InventoryItem structure
struct InventoryItem {
	char desc[DESC_SIZE] {};
	char ID[ID_SIZE] {};
	int qty {};
	double price {};
};

int main()
{
	// Create an empty Inventoryitem structure.
	InventoryItem record;

	// Open the file for binary output.
	ofstream inventory("Inventory.dat", ios::binary);

	if (inventory.is_open())
		// Write the blank records.
		for (size_t count = 0; count < NUM_RECORDS && inventory.write(reinterpret_cast<char*>(&record), sizeof(record)); ++count)
			cout << "Now writing record " << count + 1 << '\n';
	else
		cout << "Cannot open file for output\n";
}


When opening a file, you should always check that it has opened OK. Also the state should be checked after a file operation to make sure the operation has completed OK.
Topic archived. No new replies allowed.