So my following code is returning the number days in the current year, but if it's a leap year it's still returning the same number. Should I include yearNumber as a parameter in the totalDaysPastYear function? And then link it to the leapYear function, or what would be the most efficient way to do this? I think it is failing b/c currently it checks for if the month is whatever number and it doesn't look for the leapYear b/c it stops there. Should I do a && for month and year number? I have tried a few of the above and it's giving me issues, but maybe just b/c I am not formatting it correctly.
#include <iostream>
#include <iomanip>
usingnamespace std;
/**********************************************************************
* This function will prompt the user for the month. If
* the answer is in invalid then it will yield a reprompt.
***********************************************************************/
int getMonth()
{
int monthNumber;
cout << "Enter a month number: ";
cin >> monthNumber;
while (monthNumber > 12 || monthNumber < 1)
{
cout << "Month must be between 1 and 12\n"
<< "Enter a month number: ";
cin >> monthNumber;
}
return monthNumber;
}
/******************************************************************************* * gets the year
******************************************************************************/
int getYear()
{
int yearNumber;
cout << "Enter year: ";
cin >> yearNumber;
while (yearNumber < 1753)
{
cout << "Year must be 1753 or later.\n"
<< "Enter year: ";
cin >> yearNumber;
}
return yearNumber;
}
/**********************************************************************
* This function determines if the year is a leap year.
***********************************************************************/
bool isLeapYear(int yearNumber)
{
return (yearNumber % 4 == 0
&& ( yearNumber % 100 != 0 || yearNumber % 400 == 0 ) );
}
/**********************************************************************
* This function will loop and count every leap year.
***********************************************************************/
int totalLeapYears(int yearNumber)
{
int leapYears = 0;
for (int i = 1753; i <= yearNumber; i++)
{
if (isLeapYear(i)) leapYears++;
}
return leapYears;
}
/**********************************************************************
* This function will determine how many days have went by in previous
* years.
***********************************************************************/
int totalDaysPastYears(int yearNumber)
{
int yearsPassed;
int normalYears;
int totalDaysPast;
yearsPassed = yearNumber - 1753;
normalYears = yearsPassed - totalLeapYears(yearNumber);
totalDaysPast = (normalYears * 365) + (totalLeapYears(yearNumber) * 366);
return totalDaysPast;
}
/*****************************************************************************
* adds up all the days in the previous months
*****************************************************************************/
int totalDaysCurrentYear(int monthNumber)
{
int yearNumber;
if (monthNumber == 1) //days in current year previous to Jan.
return 0;
elseif (monthNumber == 2) //days in current year previous to Feb.
return 31;
elseif (monthNumber == 3) //days in current year previous to Mar.
return 31 + 28; if (isLeapYear(yearNumber)) return 31 + 29;
elseif (monthNumber == 4) //days in current year previous to Apr.
return 31 + 28 + 31; if (isLeapYear(yearNumber)) return 31 + 29 + 31;
elseif (monthNumber == 5) //days in current year prevous to May
return 31 + 28 + 31 + 30; if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30;
elseif (monthNumber == 6) //days in current year previous to Jun.
return 31 + 28 + 31 + 30 + 31;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31;
elseif (monthNumber == 7) //days in current year previous to July
return 31 + 28 + 31 + 30 + 31 + 30;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31 + 30;
elseif (monthNumber == 8) //days in current year previous to Aug.
return 31 + 28 + 31 + 30 + 31 + 30 + 31;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31 + 30 + 31;
elseif (monthNumber == 9) //days in current year previous to Sep.
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
elseif (monthNumber == 10) //days in current year previous to Oct.
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
elseif (monthNumber == 11) //days in current year previous to Nov.
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
elseif (monthNumber == 12) //days in current year previous to Dec.
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
if (isLeapYear(yearNumber))
return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
}
/*********************************************************************
* Main will call the functions above to display a month calendar of
* a given year.
*********************************************************************/
int main()
{
int monthNumber;
int yearNumber;
monthNumber = getMonth();
yearNumber = getYear();
cout << totalDaysCurrentYear(monthNumber) << endl;
return 0;
}