Inventory Program without class

I'm trying to write a program that uses a structure to store the inventory data and then manipulate them using a menu with the following tasks:
1. add new records to the file
2. display any record in the file
3. change any record in the file

Everything compiled. Issues is when viewing i am missing data that was entered.
I tried looking at all the code and seem i dont know much about advanced file operation and didn't understand the documentation as well. http://www.cplusplus.com/doc/tutorial/files/

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <fstream>  
#include <iomanip>
using namespace std;

const int size=51;
const int numRec=5;

struct Inventory
{
	char    detail[size];		//item description
	int 	quantity;
	double 	wCost,
			rCost;
	int 	month,
			day,
			year;
};

void add(Inventory);								 
void display(Inventory);								
void change(Inventory);



int main()
{
    int choice, record=1;
    char again;
    
	Inventory item = {"", 0, 0.0};   //Creating empty structures for members
	
	fstream inventory("items.dat", ios::out|ios::binary);

	//Writing blanks
	for (int count =1;count < numRec;count++)
	{
        //cout<<"Writing Blanks Records: "<<count<<endl;
        inventory.write(reinterpret_cast<char *>(&item), sizeof(item));
    }
	
	do
	{
	 cout << fixed << showpoint << setprecision(2);  
     cout<<"\nInventory Managment"<<endl;  

     cout << "Please make a selection, 1 through 4." << endl; 
     cout << "1. Add a new record" << endl;  
     cout << "2. View an exisitng record" << endl;  
     cout << "3. Change an exisitng record" << endl;  
     cout << "4. Exit" << endl;  
     cout << endl;  
     cout <<"Enter your choice (1-4): ";  
     cin >> choice; 
      
         while(choice < 1 || choice > 4)  
    
             {  
                 cout << "Invaild selection!" << endl;  
                 cout << "Please enter your choice (1-4) : "<< endl;  
                 cin >> choice;
                 
        	 }  
	
	  
       switch(choice)  
        {  

		case 1:	    cout<< "Record" <<record++<<endl;
                    add(item);  
                    break;  
        case 2:	    display(item);  
                    break;  
        case 3:	    change(item);  
                    break;  
        default:    cout << "Invalid selection" << endl;  
              
        }  
        
        cout<<"\nWould you like to go back to menu?(Y/N): ";
        cin>>again;
     }while(again=='Y'||again=='y');
                  
  
  
    inventory.close();

    system("pause");  
    return 0;  

}

 void add(Inventory item)											

 {  
     
     //Inventory item;  
     fstream inventory("item.dat",ios::out|ios::binary);

     inventory.seekp(sizeof(item), ios::end);

     cout<< "\n\nItem description: ";
     cin.ignore();
     cin.getline(item.detail, size);
	
    	cout<<"How many? Quantity: ";
    	cin>>item.quantity;
    	
    	while (item.quantity<0)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Cannot be less than 0.\n";
    		cout<<"How many? Quantity: ";
    		cin>>item.quantity;
    	}
    	
    	cout<<"Wholesale cost: $";
    	cin>>item.wCost;
    	
    	while (item.wCost<0)
    	{
    		cout<<"Cannot be less than 0.\n";
    		cout<<"Wholesale cost: $";
    		cin>>item.wCost;
    	}
    	
    	cout<<"Retail cost: $";
    	cin>>item.rCost;
    	
    	while (item.rCost<0)
    	{
    		cout<<"Cannot be less than 0.\n";
    		cout<<"Retail cost: $";
    		cin>>item.rCost;
    	}
    	
    	cout<<"What is the date added to invetory: \n";
    	cout<<"Month: ";
    	cin>> item.month;
    	
    	while (item.month<=0||item.month>12)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 1-12.\n";
    		cout<<"Month: ";
    		cin>> item.month;
    	}
    	
    	cout<<"Day: ";
    	cin>>item.day;
    	while (item.day<=0||item.day>31)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 1-31, depending on the month.\n";
    		cout<<"Day: ";
    		cin>>item.day;
    	}
    	
    	cout<<"Year: ";
    	cin>>item.year;
    	while (item.year<1900||item.year>2999) 
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 19xx-2xxx.\n";
    		cout<<"Year: ";
    		cin>>item.year;
    	}
    
    	inventory.write(reinterpret_cast<char *>(&item), sizeof(item));
    	
    	//close the file
    	inventory.close();
    	
 } 
 
 void display (Inventory item) 

 {  
     
     //binary input
     fstream inventory("item.dat", ios::in|ios::binary);
     
     //inventory.seekg (sizeof(item), ios::beg);
     inventory.read(reinterpret_cast<char *>(&item), sizeof(item));
     
     while (!inventory.eof())
     {
     cout << "Description: " << item.detail << endl;  
     cout << "Quantity: " << item.quantity << endl;  
     cout << "Wholesale cost: " << item.wCost << endl;  
     cout << "Retail price: " << item.rCost << endl;  
     cout << "Date Added: "<<item.month<<"/"<<item.day<<"/"<<item.year<<endl; 
     inventory.read(reinterpret_cast<char *>(&item), sizeof(item));
     }
     //close the file
     inventory.close();
     
 }  
 
 void change (Inventory item)		//changing record						

 {  
     int recNum;
      
     //binary input
     fstream inventory("item.dat", ios::in|ios::out|ios::binary);
     
     // Get the record number of the desired record.
	 cout << "Which record do you want to edit? ";
	 cin >> recNum;

	 // Move to the record and read it.
	 inventory.seekg(recNum * sizeof(item), ios::beg);
     inventory.read(reinterpret_cast<char *>(&item), sizeof(item));
     
     // Display 
     cout << "Description: " << item.detail << endl;  
     cout << "Quantity: " << item.quantity << endl;  
     cout << "Wholesale cost: " << item.wCost << endl;  
     cout << "Retail price: " << item.rCost << endl;  
     cout << "Date Added: "<<item.month<<"/"<<item.day<<"/"<<item.year<<endl; 
     
     // Add New Record
      cout<< "\n\nItem description: ";
     cin.ignore();
     cin.getline(item.detail, size);
	
    	cout<<"How many? Quantity: ";
    	cin>>item.quantity;
    	
    	while (item.quantity<0)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Cannot be less than 0.\n";
    		cout<<"How many? Quantity: ";
    		cin>>item.quantity;
    	}
    	
    	cout<<"Wholesale cost: $";
    	cin>>item.wCost;
    	
    	while (item.wCost<0)                                                   //Input for Sales
    	{
    		cout<<"Cannot be less than 0.\n";
    		cout<<"Wholesale cost: $";
    		cin>>item.wCost;
    	}
    	
    	cout<<"Retail cost: $";
    	cin>>item.rCost;
    	
    	while (item.rCost<0)
    	{
    		cout<<"Cannot be less than 0.\n";
    		cout<<"Retail cost: $";
    		cin>>item.rCost;
    	}
    	
    	cout<<"What is the date added to invetory: \n";                       //Input for date
    	cout<<"Month: ";
    	cin>> item.month;
    	
    	while (item.month<=0||item.month>12)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 1-12.\n";
    		cout<<"Month: ";
    		cin>> item.month;
    	}
    	
    	cout<<"Day: ";
    	cin>>item.day;
    	while (item.day<=0||item.day>31)
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 1-31, depending on the month.\n";
    		cout<<"Day: ";
    		cin>>item.day;
    	}
    	
    	cout<<"Year: ";
    	cin>>item.year;
    	while (item.year<1900||item.year>2999) 
    	{
    		cout<<"Error. Re-Type.\n";
    		cout<<"Must be between 19xx-2xxx.\n";
    		cout<<"Year: ";
    		cin>>item.year;
    	}
     
     
     // Move back to the beginning of the this record's position.
	 inventory.seekp(recNum * sizeof(item), ios::beg);

	 // Write the new record over the current record.
	 inventory.write(reinterpret_cast<char *>(&item),
		            sizeof(item));
     
     //close the file
     inventory.close();
     
 }
Note that this will loop numRec-1 times.
for (int count =1;count < numRec;count++)

Not sure but maybe you should not open the same file with multiple fstreams at the same time.
So i shouldn't do the fstream in functions but instead put everything in main?
I was more thinking that you keep inventory in main open AND call functions that opens the same file. Maybe you should close it before you enter the do-while loop.
Yeah i don't know how to make the call function open the same file.
It seem to me the item.dat is not syncing with the data i entered after closing the program. So whenever i try to view the data it goes blank.
If i try to create the blanks records with the for loop will it overwrite it when i activate the program again? Do i even need the blank records?
My main issue is adding the data with the functions without overwriting the previous entry and the data actually saving to the file.
Topic archived. No new replies allowed.