Date Validation

This program works fine, but I need help on adding proper date validation. The code that I have will only check for integers 0-9, allow dashes in mm,dd,yyyy format, and not allow any strings. I want my program to catch invalid dates and recognize leap years. Is there a way I can add to the date validation function that I have currently in my code? I am using a regular expression, so I am asking if there is any way I can add to it valid dates,and check for leap year? Here is 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// inventory.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <regex>
using namespace std;

const int DESC_SIZE = 50;

struct Date{
	int Month;
	int Day;
	int Year;
};

struct Inventory
{
	char desc[DESC_SIZE];	
	int quantity;
	double whlCost,
		   rtlCost;
	Date date;
};

void menu(std::fstream&);
bool addRecord(std::fstream&);
bool displayRecord(std::fstream&);
bool changeRecord(std::fstream&);
Date dateValidation(string);
bool isLeapYear();

int main()
{
		
	cout << fixed << showpoint << setprecision(2);
	fstream file;
	ifstream ifile("Inventory.dat");		
	if (!ifile)
	{
		Date date;
		date.Day = 1;
		date.Month = 1;
		date.Year = 1970;
		Inventory rec = {"Default", 0, 0.0, 0.0, date};
		file.open ("Inventory.dat", fstream::out | fstream::binary);
		for (int count = 0; count < 10; count++)
		{			
			file.write((char*)&rec,sizeof(rec));
		}
		cout << endl << "Warning, did not find file, so we created one.";
		cout << endl;		
		file.close();
	}	
	
	// Create Menu and run program
	menu(file);	
	system("pause");
	return 0;
	
}

Date dateValidation(string data){
	Date date;
    match_results<string::const_iterator> m;
    regex re("([0-9]+)-([0-9]+)-([0-9]*)?");	
    if (regex_match(data, m, re)) {
        date.Day = stoi(m[1].str()),
		date.Month = stoi(m[2].str()),
		date.Year = stoi(m[3].str());       
	}
	else date.Year = 0000;
	return date;
}

bool isLeapYear()
{
	Date date;

	if(date.Day%4)
		return false;
	if(date.Day%100)
		return true;
	if(date.Day%400)
		return false;
		return true;
}
	
	
void menu(std::fstream& file)
{	
	int choice = 0;
	bool success;
	while(choice!= 4) {
		cout << "Auto Zone Inventory" << endl;
		cout << endl << "1. Add new records to the file" << endl;
		cout << "2. Display any record in the file" << endl;
		cout << "3. Change any record in the file" << endl;
		cout << "4. Quit the program" << endl;
		cout << endl;
		cout << "Please enter your choice (1-4): ";
		cin >> choice;

		while (choice < 1 || choice > 4)
		{
			cout << endl << "Invalid entry. Please enter a number (1-4)" << endl;
			cin >> choice;
		}
		cout << endl;
	
		switch(choice){
			case 1:
					success = addRecord(file);					
					break;					
			case 2:
					success = displayRecord(file);
					break;
			case 3:
					success = changeRecord(file);
					break;
			case 4:	
					success = true;
					cout << "Thank you for using this program. Auto Zone cares!" << endl;
					break;
			default : 
					cout << "Error " << choice << " is not a valid number";
					break;
		}				
		if (!success) cout << "File IO Error reading file!";
		else {
			system("Pause");
			system("cls");
		}
		
	}
}

bool addRecord(std::fstream& file)
{
	cout << "Please enter new record data" << endl;
	Inventory rec;
	string dateStr;
	file.open ("Inventory.dat", fstream::out | fstream::app | fstream::binary);

	cout << endl << "Item Description:" << endl;
	cin.ignore();
	cin.getline(rec.desc, DESC_SIZE);
	cout << rec.desc << endl;
	cout << endl << "Quantity on hand:" << endl;
	cin >> rec.quantity;

	while (rec.quantity < 0)
	{
		cout <<  endl << "Invalid entry. Please enter only positive values." << endl;
		cin >> rec.quantity;
	}

	cout << endl << "Wholesale Cost" << endl;
	cin >> rec.whlCost;

	while (rec.whlCost < 0)
	{
		cout << endl << "Invalid entry. Please enter only positive values." << endl;
		cin >> rec.whlCost;
	}

	cout << endl << "Retail Cost" << endl;
	cin >> rec.rtlCost;

	while (rec.rtlCost < 0)
	{
		cout << endl << "Invalid entry. Please enter only positive values." << endl;
		cin >> rec.rtlCost;
	}
		
	bool loop = true;
	while (loop){ 
		cout << endl << "Date added to inventory (MM-DD-YYYY)" << endl;
		cin.ignore();
		cin >> dateStr;	
		rec.date = dateValidation(dateStr);		
		if (rec.date.Year != 0000) {			
			loop = false;
		}
		else 
			cout << endl << "Error Date Format is (MM-DD-YYYY)" <<endl;
	}
	file.write(reinterpret_cast<char *>(&rec),sizeof(rec));
	cout << endl << "Record added to inventory" << endl;
	file.close();
	return true;
}

bool displayRecord(std::fstream& file)
{
	Inventory rec;
	file.open ("Inventory.dat", fstream::in | fstream::binary);
	long num;
	//Figure out How many records exist
	long a = file.tellg();
	file.seekg(0, fstream::end);
	long b = file.tellg();
	long sizeFile = b - a;
	int c = sizeof(rec);
	int numRecords = (sizeFile / c);

	if (numRecords > 0){
		cout << "Please enter the record you would like to view (1-"<< (numRecords) <<")" << endl;
		cin >> num;
		if ((num >= 0) && (num <= (numRecords))){
			num = num -1;
			file.seekg(num * sizeof(rec), fstream::beg);
			file.read(reinterpret_cast<char *>(&rec), sizeof(rec));
			if (!file.eof())
			{
				cout << endl << "Here is your current record:" << endl;
				cout << endl << "Item Description: ";
				cout << rec.desc << endl;
				cout << "Quantity on hand: ";
				cout << rec.quantity << endl;
				cout << "Wholesale cost: ";
				cout << rec.whlCost << endl;
				cout << "Retail cost: ";
				cout << rec.rtlCost << endl;
				cout << "Date added to inventory: ";
				cout << rec.date.Day << "-" << rec.date.Month << "-" << rec.date.Year << endl;						
			}
			file.close();
		}
		else {
			cout << "Record Outside of Database Range" << endl;
		}
	}
	else {
		cout << "No Records Found !" << endl;
		return false;
	}
	return true;
}

bool changeRecord(std::fstream& file)
{
	Inventory rec;
	Inventory* recs;
	string dateStr;
	string yn;	
	char* buffer;	

	long num;
	
	file.open ("Inventory.dat", fstream::in | fstream::out | fstream::binary);
	
	//Figure out How many records exist
	long a = file.tellg();
	file.seekg(0, fstream::end);
	long b = file.tellg();
	long sizeFile = b - a;
	int numRecords = 0;
	if (sizeFile > 4){
		int c = sizeof(rec);
		numRecords = (sizeFile / c);
	}

	if (numRecords > 0){
		cout << "Please enter the record you would like to view (1-"<< (numRecords) <<")" << endl;
		cin >> num;
		if (!file.eof()){
			num = num -1;
			file.seekg(num * sizeof(rec), fstream::beg);
			file.read(reinterpret_cast<char *>(&rec), sizeof(rec));

			cout << endl << "Here is your current record" << endl;
			cout << endl << "Item description: ";
			cout << rec.desc << endl;
			cout << "Quantity on hand: ";
			cout << rec.quantity << endl;
			cout << "Wholesale cost: ";
			cout << rec.whlCost << endl;
			cout << "Retail cost: ";
			cout << rec.rtlCost << endl;
			cout << "Date added to inventory: ";
			cout << rec.date.Day << "-" << rec.date.Month << "-" << rec.date.Year << endl;
				
		}
		
		cout << endl << "Do you want to proceed ? (Y/N)" << endl;
		cin >> yn;
		if ((yn == "Y")||(yn == "y")){
			cout << endl << "Please enter your new data to the record"<< endl;
			cout << endl << "Item description:" << endl;
			cin.ignore();
			cin.getline(rec.desc, DESC_SIZE);
			cout << endl << "Quantity on hand:" << endl;
			cin >> rec.quantity;

			while (rec.quantity < 0)
			{
				cout << endl << "Invalid entry. Please enter only positive values." << endl;
				cin >> rec.quantity;
			}

			cout << endl << "Wholesale cost:" << endl;
			cin >> rec.whlCost;

			while (rec.whlCost < 0)
			{
				cout << endl << "Invalid entry. Please enter only positive values." << endl;
				cin >> rec.whlCost;
			}

			cout << endl << "Retail cost:" << endl;
			cin >> rec.rtlCost;

			while (rec.rtlCost < 0)
			{
				cout << endl << "Invalid entry. Please enter only positve values." << endl;
				cin >> rec.rtlCost;
			}

			bool loop = true;
			while (loop){ 
				cout << endl << "Date added to inventory (MM-DD-YYYY)" << endl;
				cin.ignore();
				cin >> dateStr;	
				rec.date = dateValidation(dateStr);		
				if (rec.date.Year != 0000) {			
					loop = false;
				}
				else 
					cout << endl << "Error Date Format is (MM-DD-YYYY)" <<endl;
			}

			file.seekp(num * sizeof(rec), fstream::beg);
			file.write(reinterpret_cast<char *>(&rec), sizeof(rec));
			cout << endl << "The Record has been edited." << endl;
		}
	}
	else cout << endl << "File contains no records!" << endl;
	file.close();
	return true;
}

A few strips of code you might like:

1
2
	const int daysInMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	const int daysInMonthLeap [] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};



1
2
3
4
5
6
bool isALeapYear( int year )
{
	/* Check if the year is divisible by 4 or divisible by 400 */
	return ((year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0));
}



1
2
3
	cin >> month >> day >> year;
	if ( month <= 0 || month > 12 || day <= 0 || day > 31 || year < 2000 || year > 2099 )
		{// code} 


Last edited on
Topic archived. No new replies allowed.