Program crash after viewing or altering a file

Ok so im trying create an program that asks the user for a few things to keep track of inventory but when i try change or view the items it crashes. i can append to the file at without problem. also i notice that first run with a clean .dat file doesnt cause a crash. so heres my 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
//Struct.h
#include <iostream>
#include <string>
using namespace std;
//Item description
//Quantity on hand
//Wholesale cost
//Retail cost
//Date added to inventory

struct DateAdded
{
	int Day;
	string Month;
	int Year;
};
struct Inventory
{
	string ItemDescription;
	int OnHandQuantity;
	double WholeSaleCost;
	double RetailCost;
	DateAdded Date;
};


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
//********************************************************************************************
//Write a program that uses a structure to store the following inventory information in a file:
//Item description
//Quantity on hand
//Wholesale cost
//Retail cost
//Date added to inventory
//
//The program should have a menu that allows the user to perform the following tasks:
//• Add new records to the file
//• Display any record in the file
//• Change any record in the file
//Input Validation: The program should not accept quantities, or wholesale or retail
//costs less than 0. The program should not accept dates that the programmer determines
//are unreasonable.
//
//*********************************************************************************************

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <cstdlib>
#include "Struct.h"
using namespace std;

void Selection1 ();
void Selection2 ();
void Selection3 ();

int main ()
{		
	//Declarations 
	int selection;

	char repeat = 'Y';
	do	
	{	
	system("CLS");
	// Program Startand initail and recurring main menu
	cout << setw(5) << "***************************************************" << endl
		 << setw(5) << "*                      Hello!                     *" << endl
		 << setw(5) << "*   This Program will take the item description,  *" << endl 
		 << setw(5) << "* on hand quantity, Wholesale cost, Retail cost   *" << endl
		 << setw(5) << "* and Date added to inventory of several items    *" << endl
		 << setw(5) << "* and write them to a file.                       *" << endl
		 << setw(5) << "***************************************************" << endl;

	cout << endl << endl ;
	cout << "What do you wish to do?:" << endl
		 << "1. Add new records to the file." << endl
		 << "2. Display a record from file." << endl
		 << "3. Change a record already on file." << endl
		 << "Selection: ";
	cin >> selection;
	
	if (selection == 1) 
		{Selection1();}
	
	else if (selection == 2)
		{Selection2();}

	else if (selection == 3)
		{Selection3();}

	cout << endl << "Would you like to make another selection? (y for yes and n for no)";
	cin  >> repeat;
	}while (toupper(repeat) == 'Y');

return 0;
}

void Selection1()
{
	struct Inventory Item;
	system("CLS");
			// Create and open the file for writing and appending.
			 fstream stuff("stuff.dat", ios::out | ios::app |ios::binary);
				if (!stuff)
					{
					cout << "Error opening file. Program aborting.\n";
					return;
					}
			 //Fills struct Item
			cout << endl;
			cout << "What is the Description of the item? ";
			cin  >> Item.ItemDescription;
			cout << "How many are on hand? ";
			cin  >> Item.OnHandQuantity;
				while(Item.OnHandQuantity<0)
				{
					cout << "Please Input a number greater than 0: ";
					cin >> Item.OnHandQuantity;
				}
			cout << "What is the whole sale cost of this item? ";
			cin  >> Item.WholeSaleCost;
				while(Item.WholeSaleCost<0)
				{
					cout << "Please Input a number greater than 0: ";
					cin >> Item.WholeSaleCost;
				}
			cout << "What is the retail cost of this item? ";
			cin  >> Item.RetailCost;
				while(Item.RetailCost<0)
				{
					cout << "Please Input a number greater than 0: ";
					cin >> Item.RetailCost;
				}
			cout << "Please type in the Date the item was entered" << endl
				 << "(EX. 19 March 1989): ";
			cin >> Item.Date.Day >> Item.Date.Month >> Item.Date.Year;
				
				//Year Validation
				while (Item.Date.Year<2005)
					{cout << "Please enter a valid year (greater than 2005): ";
					 cin  >> Item.Date.Year;}
				while (Item.Date.Year>2012)
					{cout << "Please enter a valid year (less than 2012): ";
					 cin  >> Item.Date.Year;}
				
				//Writes to file
				stuff.write(reinterpret_cast<char *>(&Item),
							sizeof(Item));
				//Closes file
				stuff.close();
}
void Selection2()
{
	system("CLS");
			//Declarations
			int ViewSelection,
				count = 1;
			struct Inventory Item;

			cout << endl << "Choose method of viewing" << endl;
			cout << "1. View All." << endl
				 << "2. Choose a number to view its information." << endl
				 << "Selection: ";
			cin  >> ViewSelection;
			
			// Create and open the file for reading.
			fstream record("stuff.dat", ios::in | ios::binary);
				if (!record)
				{
				 cout << "Error opening file. Program aborting.\n";
				 return;
				}
		if (ViewSelection == 1)
			{
			record.read(reinterpret_cast<char *>(&Item),
					   sizeof(Item));


				while (!record.eof())
				{

				cout << endl << "Record #" << count << endl;
				cout << "Description: "         ;
				cout << Item.ItemDescription << endl;
				cout << "Quantity on hand:    " ;
				cout << Item.OnHandQuantity  << endl;
				cout << "Whole Sale cost: "     ;
				cout << Item.WholeSaleCost   << endl;
				cout << "Retail cost: "         ;
				cout << Item.RetailCost      << endl;
				cout << "Date Added to record: ";
				cout << Item.Date.Day << " "
					 << Item.Date.Month << " "
					 << Item.Date.Year << endl  ;
				record.read(reinterpret_cast<char *>(&Item),
					   sizeof(Item));
				count++;
				}
				cout << endl;
			}
		if (ViewSelection == 2)
			{
				int RecordView;
				//Asks user which record they wish to view
				cout << "which record to you wish to view? ";
				cin  >> RecordView;
			
				//Displays the record
				record.seekg((RecordView-1) * sizeof(Item), ios::beg);
				record.read(reinterpret_cast<char *>(&Item),
					   sizeof(Item));
				cout << endl << "Record #" << (RecordView) << endl;
				cout << "Description: "         ;
				cout << Item.ItemDescription << endl;
				cout << "Quantity on hand:    " ;
				cout << Item.OnHandQuantity  << endl;
				cout << "Whole Sale cost: "     ;
				cout << Item.WholeSaleCost   << endl;
				cout << "Retail cost: "         ;
				cout << Item.RetailCost      << endl;
				cout << "Date Added to record: ";
				cout << Item.Date.Day << " "
					 << Item.Date.Month << " "
					 << Item.Date.Year << endl  ;
				cout << endl;
			}
		record.close();
}
void Selection3()
{
	system("CLS");
			//Declaration
			int RecordView;
			struct Inventory Item;
			//Asks users which record the wish to view and edit
			cout << "Which record do you wish to view and edit? ";
			cin  >> RecordView;

			//Displays the record
			fstream record("stuff.dat", ios::out | ios::in | ios::binary);
			if (!record)
			{
				cout << "Error opening file.";
				return;
			}
			record.seekg((RecordView-1) * sizeof(Item), ios::beg);
			record.read(reinterpret_cast<char *>(&Item),
						sizeof(Item));
				cout << endl << "Record #" << (RecordView) << endl;
				cout << "Description: "         ;
				cout << Item.ItemDescription << endl;
				cout << "Quantity on hand:    " ;
				cout << Item.OnHandQuantity  << endl;
				cout << "Whole Sale cost: "     ;
				cout << Item.WholeSaleCost   << endl;
				cout << "Retail cost: "         ;
				cout << Item.RetailCost      << endl;
				cout << "Date Added to record: ";
				cout << Item.Date.Day << " "
					 << Item.Date.Month << " "
					 << Item.Date.Year << endl  ;

			//Takes input for the new values
				cout << endl;
				cout << "Please enter the new values now." << endl
					 << "Description: ";
				cin  >> Item.ItemDescription ;
				cout << "Quantity on hand: ";
				cin  >> Item.OnHandQuantity ;
				cout << "Whole Sales Cost: ";
				cin  >> Item.WholeSaleCost ;
				cout << "Retail Cost: " ;
				cin  >> Item.RetailCost ;
				cout << "Date added: ";
				cin  >> Item.Date.Day >>
						Item.Date.Month >>
						Item.Date.Year;
				
				//Year Validation
				while (Item.Date.Year<2005)
					{cout << "Please enter a valid year (greater than 2005): ";
					 cin  >> Item.Date.Year;}
				while (Item.Date.Year>2012)
					{cout << "Please enter a valid year (less than 2012): ";
					 cin  >> Item.Date.Year;}
				
				//Writes to record
				record.seekg((RecordView-1)*sizeof(Item), ios::beg);
				record.write(reinterpret_cast<char *>(&Item),
							 sizeof(Item));
				cout << "The record has been edited!" << endl ;
				//record.close();
}
The problem is in how your writing to and reading from the file. You can't just write out the same number of bytes as the object is. The string, for example, contains things like pointers to other places in memory where the actual name is stored.
Topic archived. No new replies allowed.