If the date is invalid you do this:
date = NULL; Then you proceed to do this:
return *date; That would be dereferencing a null pointer, which is illegal and highly likely to crash your program. Of course, that entire function is one big red flag. Perhaps this would be a little better:
CTime *GetDate ( const string& requestString )
{
string inputStr;
int day, month, year;
cout << requestString << endl;
cout << "Enter Date In The Format dd/mm/yyyy: " << endl;
if ( !getline ( cin, inputStr ) )
throw runtime_error ( "Line input error" );
istringstream iss ( inputStr );
if ( !( iss>> day >> month >> year ) )
throw runtime_error ( "Formatted input error" );
return new CTime ( year, month, day, 0, 0, 0 );
} Instead of catching the exception in GetDate, require the highest level possible to catch exceptions and deal with them accordingly. It's obvious that GetDate has no idea how to handle an exception, so there's no point in catching it here. And of course, it's a good idea to look for errors in your input as well. Include for runtime_error, and instead of using ... as your catch expression, derive a more precise exception from std::exception or one of it's children and use that instead:
class InvalidDateException: public runtime_error {
public:
InvalidDateException ( const string& msg ): runtime_error ( msg ) {}
}; Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401