Inventory management file processing question problem

I have successfully worked on the file tool management for the functions but dunno how to do with file processing of sequential file txt or random access file dat. Here is the code

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#include<iostream>
#include<string.h>
#include<conio.h>

using namespace std;

struct itemEntry 
{
 float unitPrice;
 int copies;
 char name[30];
};
class Store 
{
public:
 int numItem;
 itemEntry database[100];

 Store()
 {
  numItem = 0;
 }
 void insertItem(char itemName[], int c, float p);
 void deleteItem(char itemName[]);
 itemEntry *searchi(char itemName[]);
 void updateItem(char itemName[], int total,float price);
};


void Store::insertItem(char itemName[], int c, float p) 
{
 strcpy(database[numItem].name, itemName);
 database[numItem].copies = c;
 database[numItem].unitPrice = p;
 cout << "\n\t\t ITEM INSERTED SUCCESFULLY\n";
 numItem++;

}

void Store::deleteItem(char itemName[])
{
 int i;
 for (i = 0; i < numItem; i++)
 {
  if ((strcmp(itemName, database[i].name) == 0))
  {
   database[i].copies--;
   cout<< "\n\t\t ITEM DELETED SUCCESFULLY\n";
   return;

  }
 }
 cout << "\n\t\t ITEM NOT FOUND\n";
}
itemEntry *Store::searchi(char itemName[]) 
{
 int i;
 for (i = 0; i < numItem; i++)
 {
  if ((strcmp(itemName, database[i].name) == 0))
   return &database[i];
 }
 return NULL;
}
void Store::updateItem(char itemName[], int total, float price) 
{
 itemEntry *item = searchi(itemName);
 if (item == NULL)
 {
  cout << "\n\t\t ITEM NOT FOUND\n";
  return;
  item->copies += total;
  item->unitPrice += price;
 }
}

int main() 
{
 Store sto;
 char name[100];
 int copies, unit_price,option;

 do 
 {
  cout << "\n\t------------->Tool STORE<--------------";
  cout << "\n\t\t-------------->MENU<------------";
  cout << "\n\t\t\t    1 FOR INSERT";
  cout << "\n\t\t\t    2 FOR DELETE";
  cout << "\n\t\t\t    3 FOR SEARCH";
  cout << "\n\t\t\t    4 FOR UPDATE";
  cout << "\n\t\t\t    5 FOR EXIT";
  cout << "\n\t\t  ENTER YOUR CHOICE : ";
  cin >> option;
  switch (option)
  {
  case 1: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t NO. OF COPIES : ";
    cin >> copies;
    cout << "\n\t\t\t UNIT PRICE PER ITEM : ";
    cin >> unit_price;
    sto.insertItem(name, copies, unit_price);
    break;
  case 2: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    sto.deleteItem(name);
  case 3: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    itemEntry *test;
    test = sto.searchi(name);
    if (test != NULL) 
    {
     cout << "\n\t------------->SEARCHING RESULT<--------------";
     cout << "\n\t\t\t   ITEM FOUND" << "\n\t\t\t NAME OF THE ITEM:" << test->name
      << "\n\t\t\t NUMBER OF COPIES AVAILABLE:" << test->copies
      << "\n\t\t\t UNIT PRICES PER ITEM:" << test->unitPrice;
    }
    else 
     cout << "\n\t\t\t ITEM NOT FOUND";
    break;
  case 4: cout << "\n\t\t\t ENTER DETAILS FOR UPDATE : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t ENTER TOTAL NEW ENTRY : ";
    cin >> copies;
    cout << "\n\t\t\t ENTER NEW PRICE : ";
    cin >> unit_price;
    sto.updateItem(name, copies, unit_price);
    break;
  }
 } while (option != 5);
 return 0;
}

The question is
a) Write a program that initializes the sequential file hardware.txt, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file. The tool identification number should be the record number. Use the following information to start your file

Record# Tool name Quantity Cost
3 Electric sander 7 57.98
17 Hammer 76 11.99
24 Jig saw 21 11.00
39 Lawn mower 3 79.50
56 Power saw 18 99.99
68 Screwdriver 106 6.99
77 Sledge hammer 11 21.50
83 Wrench 34 7.50

b) Repeat (a) but this time you uses the random access file hardware.dat. You should create a class for the above records and create an object for each record. When you create the random access file, you need to create 100 empty records.



I am not sure how could I have this output like this for file processing but seems that I have done most of the functions correct except for output errors when I use the update function that it has repeated outputs

Last edited on
As a first refactor, possibly consider. Note that delete seems iffy. All that's being done is decrementing the number of copies - the item isn't being deleted???

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <cstring>

using namespace std;

constexpr size_t MaxEntries {100};
constexpr size_t MaxName {30};

struct itemEntry {
	float unitPrice {};
	int copies {};
	char name[MaxName] {};
};

class Store {
public:
	size_t numItem {};
	itemEntry database[MaxEntries];

	Store() {}

	void insertItem(char itemName[], int c, float p);
	void deleteItem(char itemName[]);
	itemEntry* searchi(char itemName[]);
	void updateItem(char itemName[], int total, float price);
};

void Store::insertItem(char itemName[], int c, float p) {
	if (numItem < MaxEntries) {
		//strcpy(database[numItem].name, itemName);
		snprintf(database[numItem].name, MaxName, "%s", itemName);
		database[numItem].copies = c;
		database[numItem].unitPrice = p;
		cout << "\n\t\t ITEM INSERTED SUCCESFULLY\n";
		++numItem;
	} else
		std::cout << "Too many items\n";
}

void Store::deleteItem(char itemName[]) {
	auto* item {searchi(itemName)};

	if (item != nullptr) {
		--item->copies;
		cout << "\n\t\t ITEM DELETED SUCCESFULLY\n";
	} else
		cout << "\n\t\t ITEM NOT FOUND\n";
}

itemEntry* Store::searchi(char itemName[]) {
	for (size_t i = 0; i < numItem; ++i) {
		if ((strcmp(itemName, database[i].name) == 0))
			return &database[i];
	}

	return nullptr;
}

void Store::updateItem(char itemName[], int total, float price) {
	auto* item {searchi(itemName)};

	if (item == nullptr) {
		cout << "\n\t\t ITEM NOT FOUND\n";
		return;
	}

	item->copies += total;
	item->unitPrice += price;
}

int main() {
	Store sto;
	int option {};

	do {
		char name[MaxName] {};
		float unit_price {};
		int copies {};

		cout << "\n\t------------->Tool STORE<--------------";
		cout << "\n\t\t-------------->MENU<------------";
		cout << "\n\t\t\t    1 FOR INSERT";
		cout << "\n\t\t\t    2 FOR DELETE";
		cout << "\n\t\t\t    3 FOR SEARCH";
		cout << "\n\t\t\t    4 FOR UPDATE";
		cout << "\n\t\t\t    5 FOR EXIT";
		cout << "\n\t\t  ENTER YOUR CHOICE : ";
		cin >> option;
		cin.ignore();

		switch (option) {
			case 1:
				cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				cin.getline(name, MaxName);

				cout << "\n\t\t\t NO. OF COPIES : ";
				cin >> copies;

				cout << "\n\t\t\t UNIT PRICE PER ITEM : ";
				cin >> unit_price;

				sto.insertItem(name, copies, unit_price);
				break;

			case 2:
				cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				cin.getline(name, MaxName);
				sto.deleteItem(name);
				break;

			case 3:
				{
					cout << "\n\t\t\t ENTER NAME OF ITEM : ";
					cin.getline(name, MaxName);

					auto* test {sto.searchi(name)};

					if (test != nullptr) {
						cout << "\n\t------------->SEARCHING RESULT<--------------";
						cout << "\n\t\t\t   ITEM FOUND" << "\n\t\t\t NAME OF THE ITEM:" << test->name
							<< "\n\t\t\t NUMBER OF COPIES AVAILABLE:" << test->copies
							<< "\n\t\t\t UNIT PRICES PER ITEM:" << test->unitPrice;
					} else
						cout << "\n\t\t\t ITEM NOT FOUND";
				}
				break;

			case 4:
				cout << "\n\t\t\t ENTER DETAILS FOR UPDATE : ";
				cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				cin.getline(name, MaxName);

				cout << "\n\t\t\t ENTER TOTAL NEW ENTRY : ";
				cin >> copies;

				cout << "\n\t\t\t ENTER NEW PRICE : ";
				cin >> unit_price;

				sto.updateItem(name, copies, unit_price);
				break;
		}
	} while (option != 5);
}

@seeplus

yep but what about file processing so I can get the output like this on sequential file txt or random access file dat like this output on question a) and b) on the file, something like ifsstream as a beginner's level of file processing?

Record# Tool name Quantity Cost
3 Electric sander 7 57.98
17 Hammer 76 11.99
24 Jig saw 21 11.00
39 Lawn mower 3 79.50
56 Power saw 18 99.99
68 Screwdriver 106 6.99
77 Sledge hammer 11 21.50
83 Wrench 34 7.50



You could read the questions a and b if you want to get what I mean by sequential file or random access file
Last edited on
For a) are you expected to read the data from the file hardware.txt and process it - or just create it from data entered? If create from just data entry, then the record# will be sequential starting from 1 (or 0?).
@seeplus
Then what happens if it read the data from the file hardware.txt and process it instead of data entry like you mentioned before?
Last edited on
@seeplus

Oh sorry that I forgot to add a function which enables all list of the tools and print all the inputed cost, tool name and quantity, here is the new version including function enableItem
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include<string.h>
#include<conio.h>

using namespace std;

struct itemEntry 
{
 float unitPrice;
 int copies;
 char name[30];
};
class Store 
{
public:
 int numItem;
 itemEntry database[100];

 Store()
 {
  numItem = 0;
 }
 void insertItem(char itemName[], int c, float p);
 void deleteItem(char itemName[]);
 itemEntry *searchi(char itemName[]);
 void updateItem(char itemName[], int total,float price);
 void enableItem();
};


void Store::insertItem(char itemName[], int c, float p) 
{
 strcpy(database[numItem].name, itemName);
 database[numItem].copies = c;
 database[numItem].unitPrice = p;
 cout << "\n\t\t ITEM INSERTED SUCCESFULLY\n";
 numItem++;

}

void Store::deleteItem(char itemName[])
{
 int i;
 for (i = 0; i < numItem; i++)
 {
  if ((strcmp(itemName, database[i].name) == 0))
  {
   database[i].copies--;
   cout<< "\n\t\t ITEM DELETED SUCCESFULLY\n";
   return;

  }
 }
 cout << "\n\t\t ITEM NOT FOUND\n";
}
itemEntry *Store::searchi(char itemName[]) 
{
 int i;
 for (i = 0; i < numItem; i++)
 {
  if ((strcmp(itemName, database[i].name) == 0))
   return &database[i];
 }
 return NULL;
}
void Store::updateItem(char itemName[], int total, float price) 
{
 itemEntry *item = searchi(itemName);
 if (item == NULL)
 {
  cout << "\n\t\t ITEM NOT FOUND\n";
  return;
  item->copies += total;
  item->unitPrice += price;
 }
}

void Store::enableItem()
{
        for (int i = 0; i < numItem; i++) 
    { 
      cout << "TOOL NAME: " << database[i].name << endl; 
      cout << "QUANTITY: " << database[i].copies << endl; 
      cout << "COST: " << database[i].unitPrice << endl; 
    }
}

int main() 
{
 Store sto;
 char name[100];
 int copies, unit_price,option;

 do 
 {
  cout << "\n\t------------->Tool STORE<--------------";
  cout << "\n\t\t-------------->MENU<------------";
  cout << "\n\t\t\t    1 FOR INSERT";
  cout << "\n\t\t\t    2 FOR DELETE";
  cout << "\n\t\t\t    3 FOR SEARCH";
  cout << "\n\t\t\t    4 FOR UPDATE";
  cout << "\n\t\t\t    5 FOR ENABLE LIST";
  cout << "\n\t\t\t    6 FOR EXIT";
  
  cout << "\n\t\t  ENTER YOUR CHOICE : ";
  cin >> option;
  switch (option)
  {
  case 1: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t NO. OF COPIES : ";
    cin >> copies;
    cout << "\n\t\t\t UNIT PRICE PER ITEM : ";
    cin >> unit_price;
    sto.insertItem(name, copies, unit_price);
    break;
  case 2: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    sto.deleteItem(name);
  case 3: cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    itemEntry *test;
    test = sto.searchi(name);
    if (test != NULL) 
    {
     cout << "\n\t------------->SEARCHING RESULT<--------------";
     cout << "\n\t\t\t   ITEM FOUND" << "\n\t\t\t NAME OF THE ITEM:" << test->name
      << "\n\t\t\t NUMBER OF COPIES AVAILABLE:" << test->copies
      << "\n\t\t\t UNIT PRICES PER ITEM:" << test->unitPrice;
    }
    else 
     cout << "\n\t\t\t ITEM NOT FOUND";
    break;
  case 4: cout << "\n\t\t\t ENTER DETAILS FOR UPDATE : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t ENTER NAME OF ITEM : ";
    cin.getline(name, 80);
    cout << "\n\t\t\t ENTER TOTAL NEW ENTRY : ";
    cin >> copies;
    cout << "\n\t\t\t ENTER NEW PRICE : ";
    cin >> unit_price;
    sto.updateItem(name, copies, unit_price);
    break;
    case 5:
    sto.enableItem();
    break;
  }
 } while (option != 6);
 return 0;
}
Last edited on
OK, As a 2nd refactor, this will first read the data from hardware.txt as sequential:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <fstream>
#include <string>

constexpr size_t MaxEntries {100};

struct itemEntry {
	float unitPrice {};
	int copies {};
	std::string name;
};

struct Record {
	size_t record {};
	itemEntry ie;
};

std::istream& operator>>(std::istream& is, Record& rec) {
	if (is >> rec.record) {
		std::string name;

		while (!(is >> rec.ie.copies)) {
			std::string temp;

			is.clear();
			is >> temp;
			name += temp + ' ';
		}

		name.pop_back();
		rec.ie.name = name;

		is >> rec.ie.unitPrice;
	}
	return is;
}

class Store {
public:
	itemEntry database[MaxEntries];

	void insertItem(const std::string& name, int c, float p);
	void insertItem(const Record& rec);
	void deleteItem(const std::string& name);
	itemEntry* searchi(const std::string& name);
	void updateItem(const std::string& name, int total, float price);
	void display() const;
};

void Store::display() const {
	for (size_t i = 0; i < MaxEntries; ++i)
		if (!database[i].name.empty())
			std::cout << i << "  " << database[i].name << "  " << database[i].copies << "  " << database[i].unitPrice << '\n';

	std::cout << '\n';
}

void Store::insertItem(const Record& rec) {
	database[rec.record] = rec.ie;
}

void Store::insertItem(const std::string& name, int c, float p) {
	size_t numItem {};

	for (; !database[numItem].name.empty(); ++numItem);

	if (numItem < MaxEntries) {
		database[numItem].name = name;
		database[numItem].copies = c;
		database[numItem].unitPrice = p;
		std::cout << "\n\t\t ITEM INSERTED SUCCESFULLY\n";
	} else
		std::cout << "Too many items\n";
}

void Store::deleteItem(const std::string& name) {
	auto* item {searchi(name)};

	if (item != nullptr) {
		item->name.clear();
		std::cout << "\n\t\t ITEM DELETED SUCCESFULLY\n";
	} else
		std::cout << "\n\t\t ITEM NOT FOUND\n";
}

itemEntry* Store::searchi(const std::string& name) {
	for (size_t i = 0; i < MaxEntries; ++i) {
		if (name == database[i].name)
			return &database[i];
	}

	return nullptr;
}

void Store::updateItem(const std::string& name, int total, float price) {
	auto* item {searchi(name)};

	if (item == nullptr) {
		std::cout << "\n\t\t ITEM NOT FOUND\n";
		return;
	}

	item->copies = total;
	item->unitPrice = price;
}

int main() {
	std::ifstream ifs("hardware.txt");
	Store sto;
	int option {};

	if (ifs) {
		std::string header;

		std::getline(ifs, header);
		for (Record rec; ifs >> rec; sto.insertItem(rec));
		std::cout << "File read\n\n";
	} else
		std::cout << "File could not be read\n\n";

	do {
		std::string name;
		float unit_price {};
		int copies {};

		std::cout << "\n\t------------->Tool STORE<--------------"
			<< "\n\t\t-------------->MENU<------------"
			<< "\n\t\t\t    1 FOR INSERT"
			<< "\n\t\t\t    2 FOR DELETE"
			<< "\n\t\t\t    3 FOR SEARCH"
			<< "\n\t\t\t    4 FOR UPDATE"
			<< "\n\t\t\t    5 FOR DISPLAY"
			<< "\n\t\t\t    6 FOR EXIT"
			<< "\n\t\t  ENTER YOUR CHOICE : ";

		std::cin >> option;
		std::cin.ignore();

		switch (option) {
			case 1:
				std::cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				std::getline(std::cin, name);

				std::cout << "\n\t\t\t NO. OF COPIES : ";
				std::cin >> copies;

				std::cout << "\n\t\t\t UNIT PRICE PER ITEM : ";
				std::cin >> unit_price;

				sto.insertItem(name, copies, unit_price);
				break;

			case 2:
				std::cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				std::getline(std::cin, name);
				sto.deleteItem(name);
				break;

			case 3:
				{
					std::cout << "\n\t\t\t ENTER NAME OF ITEM : ";
					std::getline(std::cin, name);

					auto* test {sto.searchi(name)};

					if (test != nullptr) {
						std::cout << "\n\t------------->SEARCHING RESULT<--------------";
						std::cout << "\n\t\t\t   ITEM FOUND" << "\n\t\t\t NAME OF THE ITEM:" << test->name
							<< "\n\t\t\t NUMBER OF COPIES AVAILABLE:" << test->copies
							<< "\n\t\t\t UNIT PRICES PER ITEM:" << test->unitPrice;
					} else
						std::cout << "\n\t\t\t ITEM NOT FOUND";
				}
				break;

			case 4:
				std::cout << "\n\t\t\t ENTER DETAILS FOR UPDATE : ";
				std::cout << "\n\t\t\t ENTER NAME OF ITEM : ";
				std::getline(std::cin, name);

				std::cout << "\n\t\t\t ENTER TOTAL NEW ENTRY : ";
				std::cin >> copies;

				std::cout << "\n\t\t\t ENTER NEW PRICE : ";
				std::cin >> unit_price;

				sto.updateItem(name, copies, unit_price);
				break;

			case 5:
				sto.display();
				break;

			case 6:
				break;

			default:
				std::cout << "Invalid option\n";
		}
	} while (option != 6);
}

Topic archived. No new replies allowed.