One of the most frustrating aspects of using the Gregorian c
Solution
C++ program to find number of days from day 1 to day 2:
#include<iostream.h>
int daysInMonth(int month) //Method to return number of days in month, -1 if invalid month
{
int days = -1;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days = 31;
break;
case 4:
case 6:
case 9:
case 11: days = 30;
break;
case 2: days = 28;
break;
default: days = -1;
break;
}
return days;
}
int main()
{
int mon1, mon2, date1, date2;
int cday1 = 0, cday2 = 0;
int i;
// read day1
cout<<\"Enter month of first day: \";
cin>>mon1;
cout<<\"Enter date of first day: \";
cin>>date1;
// read day2
cout<<\"Enter month of second day: \";
cin>>mon2;
cout<<\"Ener date of second day: \";
cin>>date2;
//validate days
if (date1<0 || date1>daysInMonth(mon1)
|| date2<0 || date2>daysInMonth(mon2))
{
cout<<\"Invalid date\";
}
else
{
// find cumulative days from previous months for day1
for(i=1;i<mon1;i++)
{
cday1 = cday1 + daysInMonth(i);
}
cday1 = cday1 + date1;
// find cumulative days from previous months for day2
for (i=1;i<mon2;i++)
{
cday2 = cday2 + daysInMonth(i);
}
cday2 = cday2 + date2;
cout<<\"Number of days from day1 to day2 is \" <<cday2-cday1<<\"\ \"; //display difference
}
return 0;
}

