问题描述:

I've written a program, which should compares dates written in this format:

dd.mm.yyyy (German format)

for example

31.12.2019 or 06.03.2018

I've written this function to compare the dates: compare

bool compareDates(string s1, string s2) {

int day_1 = atoi(s1.substr(0, 2).c_str());

int month_1 = atoi(s1.substr(3, 2).c_str());

int year_1 = atoi(s1.substr(6, 4).c_str());

int day_2 = atoi(s2.substr(0, 2).c_str());

int month_2 = atoi(s2.substr(3, 2).c_str());

int year_2 = atoi(s2.substr(6, 4).c_str());

if (year_1 > year_2) return true;

else if (year_1 < year_2) return false;

if (month_1 > month_2) return true;

else if (month_1 < month_2) return false;

if (day_1 > day_2) return true;

else if (day_1 < day_2) return false;

return true;

}

If date1 (s1) is bigger than date2 (s2) then the function returns "true" and if not it returns "false".

I use the code like this:

if(compairDates("21.09.2016", "18.07.2019")) MessageBox::Show("true");

else MessageBox::Show("false");

If run my code now I get an error message, which tells me, that an "extern" component has thrown an uncaught exception.

My question is, why? Can somebody help me?

相关阅读:
Top