Leap Year Function

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

/*******************************
* Days of the Year
*******************************/
int daysYear(int year, int numberOfDays)
{
   numberOfDays = ((year - 1753) * 365);
   {
   if (bool isLeapYear(year) = true);
   year + 12;
   else
      return false;
   }
   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)
      return false;
   if (year % 100 != 0)
      return true;
   if (year % 400 == 0)
      return true;
   else
      return false;
}


/*******************************
* 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;
}


Any help would be greatly appreciated. Thanks!
Hello brycematheson,

better use boost date_time ( http://www.boost.org/doc/libs/1_44_0/doc/html/date_time.html ) for that task.

This
1
2
3
4
5
6
7
8
9
10
11
int daysYear(int year, int numberOfDays)
{
   numberOfDays = ((year - 1753) * 365);
   {
   if (bool isLeapYear(year) = true); // <---- ???
   year + 12; // <---- why 12?
   else
      return false; // <--- bool for int?
   }
   return numberOfDays;
}
is really nonsense

you need a loop
1
2
3
4
5
6
7
8
9
10
11
12
int daysYear(int year)
{
  int result = 0;
  const int count = (year - 1753);
  for(int i = 0; i < count; ++i)
  {
    result += 365;
    if(isLeapYear(year - i))
      ++result;
   }
   return result;
}

plus
bool isLeapYear(int year, int numberOfDays)

i didn't test it though. nor do I know whether that's the best way to do so
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

/*******************************
* Days of the Year
*******************************/
int daysYear(int year)
{
   int result = 0;
   const int 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)
      return false;
   if (year % 100 != 0)
      return true;
   if (year % 400 == 0)
      return true;
   else
      return false;
}


/*******************************
* 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;
}
Last edited on
A leap year happens every four years. It is a simple division problem.
I thought I would nominate this for "redundant comment of the week" award!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*******************************
* 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)
      return false;
   if (year % 100 != 0)
      return true;
   if (year % 400 == 0)
      return true;
   else
      return false;
}

I don't think I have ever seen one quite so good as that ;o)
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â

Any ideas?
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;
   const int count = (year - 1753);
   for(int i = 0; i < count; ++i)
   {
      result += 365;
      if (isLeapYear(year - i))
         ++result;
   }
   return result;
}
Last edited on
Galik, thank you SO much! You fixed a problem that I've been trying to work on for hours! My program now works as I had hoped!

Thank you ALL for all the help you guys have given me!
Topic archived. No new replies allowed.