Hi everyone,
I am having a little trouble finishing my programming assignment. I am trying to write a program that inputs two dates and validates them to make sure the input is ok and that the dates are between 1/1/1850 and 12/31/2100. I have gotten all of the validation down and I have written some functions that validate the date and find out if the date is a leap year. The problem I am having is the calcDays() function. It is supposed to take one of the dates and return the amount of days between the given date and 1/1/1850. I am really lost on how to do this. Any help I can get would be greatly appreciated. I have posted the code that I have so far below. Thanks in advance
Nick
#include <iostream>
using namespace std;
void getDate(int &month, char &first, int &day, char &second, int &year);
bool validDate(int month, char first, int day, char second, int year);
int calcDays(int month, int day, int year);
bool isLeapYear(int year);
int main ()
{
int yeara, yearb, montha, monthb, daya, dayb;
char first, second;
do
{
cout << "Enter first date (yyyy-mm-dd): ";
getDate(montha, first, daya, second, yeara);
}
while(!validDate(montha, first, daya, second, yeara));
do
{
cout << "Enter second date (yyyy-mm-dd): ";
getDate(monthb, first, dayb, second, yearb);
}
while(!validDate(monthb, first, dayb, second, yearb));
cout << "DBD:";
cout << calcDays(montha, daya, yeara) - calcDays(monthb, dayb, yearb);
cout << endl;
return 0;
}
void getDate(int &month, char &first, int &day, char &second, int &year)
{
cin >> year >> first >> month >> second >> day;
}
bool validDate(int month, char first, int day, char second, int year)
{
bool date = true;
int numDays = 0;
if (month < 1 || month > 12)
{
cout << "Bad month:" << month << endl;
date = false;
}
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
numDays = 31;
break;
case 9: case 4: case 6: case 11:
numDays = 30;
break;
case 2:
numDays = 28;
break;
}
if(month == 2 && isLeapYear(year))
numDays++;
if (day > numDays || day < 1)
{
cout << "Bad day for " << month << '/' << year << ':' << day << endl;
date = false;
}
if (year < 1850 || year > 2150)
{
cout << "Bad year: " << year << endl;
date = false;
}
if (first !='-' || second !='-')
{
cout << "Use only dash \'-\': " << first << ":" << second << endl;
date = false;
}
return date;
}
int calcDays(int month, int day, int year)
{
// HERE IS WHERE I NEED HELP!
}
bool isLeapYear(int year)
{
bool leapyear = false;
if (year % 4 == 0)
leapyear = true;
if (year % 100 == 0)
leapyear = false;
if (year % 400 == 0)
leapyear = true;
return leapyear;
}