I am relatively new to C++ and am a bit unsure why the following function doesn't work.
/*********************************************************
* GetDate: Takes a string to be displayed to the user
* and returns a CTime object. Returns NULL if an invalid
* date is entered
*********************************************************/
CTime GetDate(string requestString)
{
string inputStr;
int day;
int month;
int year;
cout << requestString << endl;
cout << "Enter Date In The Format dd/mm/yyyy" << endl;
cin >> inputStr;
day = atoi(inputStr.substr(0, 2).c_str());
month = atoi(inputStr.substr(3, 2).c_str());
year = atoi(inputStr.substr(6, 4).c_str());
CTime *date;
try
{
date = new CTime(year, month, day, 0, 0, 0);
}
catch(...)
{
date = NULL;
}
return *date;
}
The function crashes if and invalid date is input (32/12/2000) even though I have used a try catch statement.
Could somebody explain why this doesn't work please?
Thanks
James