Code works, but screen closes right after I run.

I'm on Visual Studio --> general --> Empty Project. What could be the problem? I even used a system("pause") on line ~50

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;

struct Inven
{
	string Desc,		// Item description
		   Date;		// Date added to inventory
	int    Qty;			// Quantity on hand
	double WhsleCost,	// Wholesale cost
		   RetailCost;	// Retail cost
};

// Function prototype
void addRecord(Inven &, fstream &);
void displayRecord(Inven &, fstream &);
void changeRecord(Inven &, fstream &);
long byteNum(int);
void displayError();
bool validDate(string);

int main ()
{
	Inven record;// = {" ", " ", 0, 0.0, 0.0};
	int Input;

	fstream File("inventory.dat", ios::out | ios::in | ios::binary);
	if (!File)
	{
		cout << "Error opening file.\n";
		return 0;
	}

	cout << "               Inventory program menu.\n"
		 << "Choose one of the following numbers to perform a task:\n";
	cout << " 1. Add new records to the file.\n";
	cout << " 2. Display any record in the file.\n";
	cout << " 3. Change any record in the file.\n";
	cin  >> Input;

	switch (Input)
	{
		case 1 : addRecord(record, File);
				 break;
		case 2 : displayRecord(record, File);
				 break;
		case 3 : changeRecord(record, File);
	}

	File.close();
system("pause");
	return 0;
}

//********************************************************************************
// addRecord                                                                     *
// This function accepts an Inven structure and a fstream object as its          *
// arguments. The user is asked to input the record information that is          *
// validated and sotred in the file.                                             *
//********************************************************************************
void addRecord(Inven &record, fstream &File)
{
	File.seekp(0L, ios::end);
	cout << "Enter the following inventory information:\n";
	cout << "Item description: ";
	cin.ignore();
	getline(cin, record.Desc);
	do
	{
		cout << "Date in the format MM/DD/YYYY: ";
		cin.ignore();
		getline(cin, record.Date);

		if (validDate(record.Date) == 0)
		{
			cout << validDate(record.Date) << endl;
			cout << "Error! Invalid date format.\n";
		}

	} while (validDate(record.Date) == 0);
	do
	{
		cout << "Quantity :";
		cin  >> record.Qty;
		if (record.Qty < 0)
			displayError();
	} while (record.Qty < 0);
	do
	{
		cout << "Wholesale cost: ";
		cin  >> record.WhsleCost;
		if (record.Qty < 0)
			displayError();
	} while (record.WhsleCost < 0);
	do
	{
		cout << "Retail cost: ";
		cin  >> record.RetailCost;
		if (record.RetailCost < 0)
			displayError();
	} while (record.RetailCost < 0);
	File.write(reinterpret_cast<char *>(&record), sizeof(record));
}

//********************************************************************************
// displayRecord                                                                 *
// This function accepts an Inven structure and a fstream object as its          *
// arguments. The user is ask to input the number of the record to read and      *
// display from the file.                                                        *
//********************************************************************************
void displayRecord(Inven &record, fstream &File)
{
	int recNum;

	cout << "Enter the record number: ";
	cin  >> recNum;
	if (recNum < 1)
		recNum = 1;
	recNum--;
	File.seekg(byteNum(recNum), ios::beg);
	File.read(reinterpret_cast<char *>(&record), sizeof(record));
	cout << "Record number: " << (recNum + 1) << endl;
	cout << "Item description: ";
	cout << record.Desc << endl;
	cout << "Date : ";
	cout << record.Date << endl;
	cout << "Quantity :";
	cout << fixed << showpoint << setprecision(2);
	cout << record.Qty << endl;
	cout << "Wholesale cost: ";
	cout << record.WhsleCost << endl;
	cout << "Retail cost: ";
	cout << record.RetailCost << endl;
}

//********************************************************************************
// changeRecord                                                                  *
// This function accepts an Inven structure and a fstream object as its          *
// arguments. The user is asked to input the number of the record and the        *
// information to change in the file.                                            *
//********************************************************************************
void changeRecord(Inven &record, fstream &File)
{
	int recNum;

	cout << "Enter the record number: ";
	cin  >> recNum;
	if (recNum < 1)
		recNum = 1;
	recNum--;
	File.seekg(byteNum(recNum), ios::beg);
	File.read(reinterpret_cast<char *>(&record), sizeof(record));
	cout << "Enter the following inventory information:\n";
	cout << "Item description: ";
	cin.ignore();
	getline(cin, record.Desc);
	do
	{
		cout << "Date in the format MM/DD/YYYY: ";
		cin.ignore();
		getline(cin, record.Date);
		if (!validDate(record.Date))
		{
			cout << validDate(record.Date) << endl;
			cout << "Error! Invalid date format.\n";
		}
	} while (!validDate(record.Date));
	do
	{
		cout << "Quantity :";
		cin  >> record.Qty;
		if (record.Qty < 0)
			displayError();
	} while (record.Qty < 0);
	do
	{
		cout << "Wholesale cost: ";
		cin  >> record.WhsleCost;
		if (record.Qty < 0)
			displayError();
	} while (record.WhsleCost < 0);
	do
	{
		cout << "Retail cost: ";
		cin  >> record.RetailCost;
		if (record.RetailCost < 0)
			displayError();
	} while (record.RetailCost < 0);
	File.seekp(byteNum(recNum), ios::beg);
	File.write(reinterpret_cast<char *>(&record), sizeof(record));
}

//********************************************************************************
// byteNum                                                                       *
// This function accepts an integer as its argument. Returns the byte number in  *
// the file of the record whose number is passed as the argument.                *
//********************************************************************************
long byteNum(int recNum)
{
	return sizeof(Inven) * recNum;
}

//********************************************************************************
// diplayError                                                                   *
// This function displays an input validation error message.                     *
//********************************************************************************
void displayError()
{
	cout << "Error! number must be greater than 0.\n";
}
//********************************************************************************
// validDate                                                                     *
// This function accepts a string as its argument and test its contents for a    *
// valid date format.                                                            *
//********************************************************************************
bool validDate(string date)
{
	if ( date.length() == 9)
		date = "0" + date;

	if (date.length() != 10)
		return false;
	
	for (int i = 0; i < date.length(); i++)
	{
		if (i == 2 || i == 5)
		{
			if (date[i] != '/')
			{
				return false;
			}
			continue;
		}

		if (!isdigit(date[i]))
			return false;
	}
	return true;
Last edited on
Hello mike0910,

See message http://www.cplusplus.com/forum/beginner/1988/

Andy
closed account (E0p9LyTq)
I'd suspect your program is unable to open your inventory file. Your logic immediately terminates the program if unable to open the file, for whatever reason (lines 30-35).
So what should I change or add to fix this problem?
Topic archived. No new replies allowed.