Getting Abort() has been called error:

I am trying to run below provided code to find the difference between 2 dates and getting error: Abort() has been called. Can someone please help me to fix this?

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
string Date_Diff(string,string); //function prototypes
string Add_Days(string,string);
string Sub_Days(string,string);
int mnthly_days(int,int,int,int);
int DaysInMonth(int);

int main()
{

ifstream myfile;
/*cout<<"enter file name:"<<endl;
cin>>file;*/

myfile.open("date.txt"); //opens the source file
string str1,str2,str3;
while(!myfile.eof())
{
myfile>>str1; //reads 3 strings in a line
myfile>>str2;
myfile>>str3;
if(str2=="to") //checks which operation has to be performed and calls functions accordingly
cout<<Date_Diff(str1,str3)<<" days"<<endl;
else if(str2=="+")
cout<<Add_Days(str1,str3)<<endl;
else if(str2=="-")
cout<<Sub_Days(str1,str3)<<endl;
}
myfile.close();
system("pause");
return 0;
}

string Date_Diff(string str1,string str3)
{
string mnth1=str1.substr(0,2); //substring function here splits the date string into individual date, month and year forms
string mnth2=str3.substr(0,2);
string day1=str1.substr(3,2);
string day2=str3.substr(3,2);

int m1,m2,d1,d2;
istringstream(mnth1)>>m1; //istringstream is used to parse strings to integers
istringstream(mnth2)>>m2;
istringstream(day1)>>d1;
istringstream(day2)>>d2;
int days;

if(m1<m2)
days=mnthly_days(m1,m2,d1,d2);
else if(m1>m2)
{
int var1,var2;
var1=m1;m1=m2;m2=var1;
var2=d1;d1=d2;d2=var2;
days=(mnthly_days(m1,m2,d1,d2))*(-1);
}
else if(m1==m2)
days=d2-d1;

string s;
ostringstream d_s; //ostringstream is used to parse integer to string
d_s<<days;
s=d_s.str(); //str() is used to convert stringstream to string
return s;
}


int mnthly_days(int m1,int m2,int d1,int d2) //calculates the number of days of the incomplete years of the period of dates
{
int sum;


int sum_1=DaysInMonth(m1)-d1; //sum_1 calculates the number of days of 1st incomplete year
int sum_2=0;
while(m1<12)
{
m1++;
sum_1=sum_1+DaysInMonth(m1);
}
for(int i=1;i<m2;i++)
{
sum_2=sum_2+DaysInMonth(i); //sum_2 calculates the number of days of last incomplete year
}
sum_2=sum_2+d2;
sum=sum_1+sum_2;




sum=DaysInMonth(m1)-d1;
for(int i=m1+1;i<m2;i++)
{
sum=sum+DaysInMonth(i);
}
sum=sum+d2;

return sum;
}


string Add_Days(string str1,string str3) //adds the days to the start date and calculates the final date
{
string mnth1=str1.substr(0,2);
string day1=str1.substr(3,2);

int m1,m2,d1,d2,n;
m2=0;d2=0;
istringstream(mnth1)>>m1;
istringstream(day1)>>d1;

istringstream(str3)>>n;
if(n<=(DaysInMonth(m1)-d1))//compares the number of days to be added and the number of days in the month and calculates accordingly
{
d2=d1+n;
m2=m1;

}
else
{
n=n-(DaysInMonth(m1)-d1);
if(m1==12)
{
m2=1;

}
else
{
m2=m1+1;

}
while(n>DaysInMonth(m2))
{
n=n-DaysInMonth(m2);
if(m2==12)
{
m2=1;

}
else
{
m2=m2+1;

}
}
d2=n;
}
string str,m_2,d_2;
ostringstream month2_s; //ostringstream is used to parse integer to string stream
ostringstream day2_s;

day2_s<<d2;
month2_s<<m2;

m_2=month2_s.str(); //str() function is used to convert string stream to string
d_2=day2_s.str();

str=m_2+"-"+d_2; //strings are concatenated
return str;

}

string Sub_Days(string str1,string str3) //subtracts no. of days from a date and calculates the final date
{
string mnth1=str1.substr(0,2);
string day1=str1.substr(3,2);

int m1,m2,d1,d2,n;
m2=0;d2=0;
istringstream(mnth1)>>m1; //istringstream is used to parse string to integer
istringstream(day1)>>d1;
istringstream(str3)>>n;

while(n>DaysInMonth(m2))
{
n=n-DaysInMonth(m2);
if(m2==1)
{
m2=12;

}
else
{
m2=m2-1;

}
}
d2=DaysInMonth(m2)-n;

string str,m_2,d_2;
ostringstream month2_s; //ostringstream is used to parse integer to string
ostringstream day2_s;

day2_s<<d2;
month2_s<<m2;

m_2=month2_s.str(); //str() function is used to convert string stream to string
d_2=day2_s.str();

str=m_2+"-"+d_2;
return str;
}

int DaysInMonth(int m) //returns the number of days in a month by checking the month and the year
{
int d;
switch(m)
{
case 1: d=31; break;
case 2: d=28;
break;
case 3: d=31; break;
case 4: d=30; break;
case 5: d=31; break;
case 6: d=30; break;
case 7: d=31; break;
case 8: d=31; break;
case 9: d=30; break;
case 10: d=31; break;
case 11: d=30; break;
case 12: d=31; break;
}
return d;
}
Topic archived. No new replies allowed.