hi,
I'm checking a date is valid as it's typed in, however I always want to allow the user to skip with a CR.
The while condition is causing my program to crash when str_date.length()==0 (I think)
here's my code:
struct Date {
int d;
int m;
int y;
}
Date getDate() {
string str_date;
Date date;
do {
cout << "\nEnter DOB (DDMMYYYY): ";
getline(cin, str_date);
cout << str_date.length(); // sanity check
if (str_date.length()==0) break;
if (atoi(str_date.c_str()) && (str_date.length() == 8)) break;
} while (!atoi(str_date.c_str()) || (str_date.length() != 8));
// Date OK so save
date.d = atoi(str_date.substr(0,2).c_str());
date.m = atoi(str_date.substr(2,3).c_str());
date.y = atoi(str_date.substr(4,7).c_str());
return date;
}