I'm writing a program to determine the number of days between a user selected date and the year 1753 (because before 1753, they weren't using the Gregorian calendar.) I'm not having a problem writing the code to determine whether or not it is a leap year, but I'm having trouble figuring out how to find out how MANY leap years there are between the two dates and then somehow adding that to the rest of the years. Could someone give my brain a jumpstart? Here's the code I have so far:
#include <iostream>
usingnamespace std;
/*******************************
* Days of the Year
*******************************/
int daysYear(int year, int numberOfDays)
{
numberOfDays = ((year - 1753) * 365);
{
if (bool isLeapYear(year) = true);
year + 12;
elsereturnfalse;
}
return numberOfDays;
}
/*******************************
* Is Leap Year Function:
*
* isLeapYear(year)
* if year % 4 != 0
* return false
* if year % 100 != 0
* return true
* if year % 400 = 0
* return true
* else
* return false
*******************************/
bool isLeapYear(int year, int numberOfDays)
{
if (year % 4 != 0)
returnfalse;
if (year % 100 != 0)
returntrue;
if (year % 400 == 0)
returntrue;
elsereturnfalse;
}
/*******************************
* Main
*******************************/
int main()
{
//Initializing Variables
int year;
int numberOfDays;
//Prompt user for year
cout << "Year: ";
cin >> year;
numberOfDays = daysYear(year, numberOfDays);
//Put number of days since year on screen
cout << "Number of days: " << numberOfDays << endl;
return 0;
}
The values I put in there originally such as if (bool isLeapYear(year) = true); and year + 12; were just kind of test values to see if I could get it to work. I'm pretty new to C++ and was just playing around with some things to see if I could get it to work. I'm now struggling again, though.
Coder777, I put in the edited code that you replied with, but I'm still having troubles. I edited the values a little bit, but I still can't seem to get my code working:
#include <iostream>
usingnamespace std;
/*******************************
* Days of the Year
*******************************/
int daysYear(int year)
{
int result = 0;
constint count = (year - 1753);
for(int i = 0; i < count; ++i)
{
result += 365;
if (bool isLeapYear(year - i))
++result;
}
return result;
}
/*******************************
* Is Leap Year Function:
*
* isLeapYear(year)
* if year % 4 != 0
* return false
* if year % 100 != 0
* return true
* if year % 400 = 0
* return true
* else
* return false
*******************************/
bool isLeapYear(int year)
{
if (year % 4 != 0)
returnfalse;
if (year % 100 != 0)
returntrue;
if (year % 400 == 0)
returntrue;
elsereturnfalse;
}
/*******************************
* Main
*******************************/
int main()
{
//Initializing Variables
int year;
int result;
//Prompt user for year
cout << "Year: ";
cin >> year;
result = daysYear(year);
//Put number of days since year on screen
cout << "Number of days: " << result << endl;
return 0;
}
Haha! Trust me, I agree. My teacher is trying to get us to write psuedocode in our comments though. Sounds very redundant to me as well, but I can't argue with him when he holds my grade in his hands.
Currently, I'm having a problem with the above code that Coder777 suggested. I cannot get it to compile no matter what I do. I get the error(s):
assignment13.cpp: In function âint daysYear(int)â:
assignment13.cpp:30: error: expected primary-expression before âboolâ
assignment13.cpp:30: error: expected `)' before âboolâ
Ah, that makes more sense. I will let you off the redundant comment award in that case.
You need to either declare a prototype to your function bool isLeapYear(int year); before you use it or you need to move that function definition above the function that uses it. The bool keyword should not have been there at line 14.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// prototype declaration
bool isLeapYear(int year);
/*******************************
* Days of the Year
*******************************/
int daysYear(int year)
{
int result = 0;
constint count = (year - 1753);
for(int i = 0; i < count; ++i)
{
result += 365;
if (isLeapYear(year - i))
++result;
}
return result;
}