Date Validation Help

How would I validate the date? Thank you.

struct Inventory
{
char SKU[SKU_SIZE];
char category[CAT_SIZE];
int qty;
double cost;
char date[CAT_SIZE];
};

void enterRecords(fstream &)
{
cout << "Enter Following Inventory Data" << endl;
Inventory Info;
fstream Inventory("test.dat", ios::out | ios::binary | ios::app);
cout << "SKU (Stock Keeping Unit): ";
cin.ignore();
cin.getline(Info.SKU, SKU_SIZE);
cout << "Category: ";
cin >> Info.category;
cout << "Quantity On Hand: ";
cin >> Info.qty;
cout << "Wholesale Cost: ";
cin >> Info.cost;
cout << "Date Added To Inventory: ";
cin >> Info.date;
Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
cout << "Record Added To File" << endl << endl;
if(Inventory.fail())
Inventory.clear();
Inventory.close();
}
First, you need to define what is the acceptable format for your date. For example 12/13/13 is valid if MM/DD/YY, but not valid if DD/MM/YY or YY/MM/DD. Do you accept months as numbers or full name, on 3 letters? Is year a two digit or a 4 digit number? What separators do you accept (space, -, /)?
Oh, sorry about that. It should be in MM/DD/YY format. So the separator is "/".
Ok:
1. Find first instance of "/" character. If not found, date is not valid and exit
2. Get the substring from 0 to position of "/" as month_string, and the rest as rest_string.
3. Try to convert month_string to an integer month, and check if 1<=month<=12. If any is wrong, your date is invalid and exit.
4. Get the rest_string, find "/". If not found, date is invalid
5. Get day and year strings, similar to month string.
6. If year is not between 0 and 99 date is invalid
7. If day<1 date is invalid
8. Do a switch by month to get the upper limit on days. Remember, February has 28 days, unless ((year%4 ==0) && (year>0))
How do I do #2. substr() won't work for me. I assume it's because I'm comparing a char struct? How would I convert Info.date into a string so i could get it? Thanks.

Nvm: I think I got it. Thanks a lot!
Last edited on
Topic archived. No new replies allowed.