I'm unfamiliar with how to write recursive functions. Can anyone help me out here?

I need a recursive function to see if date number 1 is greater than date number 2. If it is it returns true. If 2 > 1, it switches them and returns them like that. If the dates fall in the same year it subtracts them right then and gives me the amount of days between the two.

So far here is my code:

``````#include <iostream>
using namespace std;

int isFirstDateGreaterThanSecond();
int getDayNumber(int day1, int month1, int year1);
int getDaysBetweenDates(int day, int month, int year, int day2, int month2, int year2);

const int SENTINEL = 1799;

void main()
{
int year1, month1, day1, year2, month2, day2;

cout << "This program finds the day number of a certain date entered by the user, and the number of days between it and another input date given by the user." << endl;
cout << "Please enter a date between 1800 and 2100 in order of month, day, year. (ex: 6 25 1987) . Enter 1799 in any parameter to exit the program." << endl;
cin >> month1 >> day1 >> year1;

if(month1 == SENTINEL || day1 == SENTINEL || year1 == SENTINEL) return;

while(month1 < 1 || month1 > 12 || day1 < 1 || day1 > 31 || year1 < 1800 || year1 > 2100)
{
cout << "You have entered an invalid date. Please try again. Enter 1799 to exit the program." << endl;
cin >> month1 >> day1 >> year1;
}
cout << "Please enter another date (ex: 2 28 2007)." << endl;
cin >> month2 >> day2 >> year2;

int getDaysBetweenDates(day1, month1, year1, day2, month2, year2);

}

int getDaysBetweenDates(int day, int month, int year, int day2, int month2, int year2)
{
if(isFirstDateGreaterThanSecond())
{
getDayNumber(day, month, year);
}

}

int getDayNumber(int day, int month, int year)
{

int a, y, m, year, month, yearBefore, day;
int day1 = 0;
int julianDay = 0;

cin>>month>>day>>year;

for(yearBefore=year; yearBefore >= (year-1); yearBefore--)
{
a = (14 - month) / 12;
y = yearBefore + 4800 - a;
m = month + 12 * a - 3;

julianDay = day + (153 * m + 2) / 5 + y *  365 + y / 4 - y/100 + y/400 - 32045;

if(day1==0)
{
day1 += julianDay;
}
else
{
day1 -= julianDay;
}

day = 31;
month = 12;
}
return day1;

}

int isFirstDateGreaterThanSecond(int day1, int month1, int year1,int day2, int month2, int year2)

{
return 1;

}``````

Just a push in the right direction would be appreciated!

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by ArkM

I'm not sure why you are considering recursion for this problem. It's not appropriate here.

``````int dayNum1 = getDayNumber(date1);
int dayNum2 = getDayNumber(date2);
int dayDiff;
if (dayNum1 > dayNum2)
{
dayDiff = dayNum1 - dayNum2;
}
else
{
dayDiff = dayNum2 - dayNum1;
}
return dayDiff;``````

Do you need anything more complex than that?

Keep it simpler:

``return abs(getDayNumber(date1) - getDayNumber(date2));``
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.