Calculate the Days Between Two Dates?

This program should calculate the number of days between two months after validating that the inputs are actually dates. It is assumed that the second date is after the first one. I don't have to worry about years. So far I have everything except the actual calculation function finished. I'm wondering if I just have to do a ton of if statements or if there is a simpler way to go about this? I have one example of the function already typed out, the part I have written will calculate the number of days if the months are the same.

#include <iostream>
bool valid_date(int day, int month);
int calc_days(int day1, int month1, int day2, int month2);
using namespace std;
int main()
{
int month1, month2, day1, day2, calculate;
int calc_days(int day1, int month1, int day2, int month2);
bool valid;
do
{
cout << "Please enter the numbers corresponding to a month and day" << endl;
cin >> month1 >> day1;
bool valid_date(int day1, int month1);
valid = valid_date(day1, month1);
} while (valid == false);
do
{
cout << "Please enter the numbers corresponding to a later month and day" << endl;
cin >> month2 >> day2;
bool valid_date(int day2, int month2);
valid = valid_date(day2, month2);
} while (valid == false);
calculate = calc_days(day1, month1, day2, month2);
}
bool valid_date(int day, int month)
{
if (month > 0 && month<13)
{
if (month = 1, 3, 5, 7, 8, 10, 12)
{
if ((day>0) && (day<32))
{
cout << "Valid Input" << endl;
return true;
}
else
{
cout << "Invalid Input" << endl;
return false;
}
}
else if (month = 2)
{
if ((day>0) && (day<29))
{
cout << "Valid Input" << endl;
return true;
}
else
{
cout << "Invalid Input" << endl;
return false;
}
}
else if (month = 4, 6, 9, 11)
{
if ((day>0) && (day<31))
{
cout << "Valid Input" << endl;
return true;
}
else
{
cout << "Invalid Input" << endl;
return false;
}
}
else
{
cout << "Invalid Input" << endl;
return false;
}
}
else
{
cout << "Invalid Input" << endl;
return false;
}
}
int calc_days(int day1, int month1, int day2, int month2)
{
int days;
if (month1 = month2);
{
days = (day2 - day1);
cout << "There are " << days << " days between the first and second date" << endl;
return 0;
}

}
Last edited on
Topic archived. No new replies allowed.