check a string for proper date input PLEASE HELP

I posted this question earlier and I am still completely stuck. I know it is alot of code to look at but the problem should be simple I just can't figuare it out for the life of me.

I need to test that the user input the date in the proper format dd/mm/yyyy. how do I test the string upon enter to ensure proper formatting? the char's are all converted to int's and the pointers are there but how can I test the string? I believe it needs to be done in the String_To_MDY()....

[code]
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>

using namespace std;

struct DATE_STRUCT
{
int month,
day,
year;
};

void String_To_MDY(char*, DATE_STRUCT&);
bool Validate_Date(DATE_STRUCT&);
char* Strchcpy(char*, char*, int);

int main()
{
char date_string [11];
DATE_STRUCT mdy_date;
char response[2];
cout << endl << endl;
cout << "Do you want to convert and validate a date?(Y/N): ";
cin.getline(response, 2);

while (toupper(*response) == 'Y')
{
cout << endl;
cout << "Enter the date in mm/dd/yyyy form: ";
cin.getline(date_string, 11);

String_To_MDY (date_string, mdy_date);

cout << endl;
cout << "The converted date is the following:" << endl << endl;
cout << "Month:" << setw(4) << mdy_date.month << endl;
cout << "Day: " << setw(4) << mdy_date.day << endl;
cout << "Year: " << setw(4) << mdy_date.year << endl;

if ( Validate_Date(mdy_date) )
{
cout << endl;
cout << "The date is valid.";
}
else
{
cout << endl;
cout << "The date is invalid.";
}

cout << endl << endl;
cout << "Do you want to convert and validate a date?(Y/N): ";
cin.getline(response, 2);

}
cout << endl;
return 0;
} // End of main()

void String_To_MDY(char* date_ptr, DATE_STRUCT& date_struct)
{
char month[3],
day[3],
year[5];
char* ch_ptr;

ch_ptr = date_ptr;

ch_ptr = Strchcpy(month, ch_ptr, '/');
++ch_ptr;
ch_ptr = Strchcpy(day, ch_ptr, '/');
++ch_ptr;
Strchcpy(year, ch_ptr, '\0');
date_struct.month = atoi(month);
date_struct.day = atoi(day);
date_struct.year = atoi(year);


return;
} // End of String_To_MDY()

char* Strchcpy(char* target, char* source, int ch)
{
while (*source != ch && *source != '\0')
{
*target = *source;
++target;
++source;
}

*target = '\0';

return source;
[code]
dd/mm/yyyy

So you need to check that the first two input characters are numbers, and that they're something between 01 and 31. How would you do that by hand? You say you can already turn the characters into int, so:

Look at first int.
If it's not 0 or 1 or 2 or 3, then this is bad input.
Look at second int.
If it's not 0 or 1 or 2 or 4 or 5 or 6 or 7 or 8 or 9, then this is bad input.
If the first number was 3, and the second number is not 0 or 1, then this is bad input.

Look at third input character. If it's not '/', then this is bad input.

Look at third int (first part of month).
If it's not 0 or 1, then this is bad input.
Look at fourth int.
If it's not 0 or 1 or 2 or 4 or 5 or 6 or 7 or 8 or 9, then this is bad input.
If the third numbers was 1, and the second number is not 0 or 1 or 2, then this is bad input.


And so on.
ok this is what I have after the String_To_MDY am I getting closer? this still allows the user to but in a false date structure?

bool Validate_Date(DATE_STRUCT& date_struct)
{
int ny_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (date_struct.month < 1 || date_struct.month > 12)
return false;

if (date_struct.day < 1)
return false;
if (date_struct.year % 4 == 0)
{
if (date_struct.day > ly_days[date_struct.month])
return false;
}
else
if (date_struct.day > ny_days[date_struct.month])
return false;

if (date_struct.year < 1980)
return false;

return true;
Topic archived. No new replies allowed.