trying to add leap year into program

I'm not really sure as to how to add leap year in here. I looked on wikipedia and got the pseudocode but I still don't understand.

Thanks

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
int isLeapYear(int yearPassedFromDaysYears)
{
   int holderV = 0;

  

   return holderV;
}

int daysYear(int yearPassedFromMain)
{
   int holderV = 0;

   while ((yearPassedFromMain - 1753) > 0)
   {
      holderV = holderV + 365;
      yearPassedFromMain --;
   }

   return holderV;
}

int main()
{
   int year = 0;
   int days = 0;

   cout << "Year: ";
   cin >> year;

   days = daysYear(year);
   cout << "Number of days: " << days;

   return 0;
}
I got the algorithm from wikipedia. It states:

1
2
if (year modulo 400 is 0) or ( (year modulo 4 is 0) and (year modulo 100 is not 0) )
       then is_leap_year

Here is how you could implement a function like that:

1
2
3
4
bool isLeapYear(int year)
{
	return (!(year % 400) || (!(year % 4) && year % 100));
}

You want a function like isLeapYear() to return a bool, not an int, since all you want is a true or false check. Note that I'm using an int as a bool. Any nonzero int will return true when used as a bool, and 0 will return false. That could have been written as

1
2
3
4
bool isLeapYear(int year)
{
	return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

which might be clearer for you to visualize what's going on.

If isLeapYear() returns true, add 366 instead of 365 to your variable. I'm assuming you want a function that returns the number of days passed since 1753, for some reason. You could write it like this:

1
2
3
4
5
6
7
8
9
10
11
int daysSince1753(int year)
{
	int days = 0;

	for(int i = year; i > 1753; --i)
	{
		days += isLeapYear(i) ? 366 : 365;
	}

	return days;
}

Of course, you should check for errors (an argument that is < 1753) and it would be better to place your constant in a const int.
Last edited on
So would it look something like this? :
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
bool isLeapYear(int year)
{
   return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

int daysSince1753(int year)
{
   int days = 0;

   for(int i = year; i > 1753; --i)
   {
      days += isLeapYear(i) ? 366 : 365;
   }

   return days;
}

int daysYear(int yearPassedFromMain)
{
   int holderV = 0;

   while ((yearPassedFromMain - 1753) > 0)
   {
      holderV = holderV + isLeapYear(yearPassedFromMain);
      yearPassedFromMain --;

      -1;
   }

   return holderV;
}

int main()
{
   int year = 0;
   int days = 0;

   cout << "Year: ";
   cin >> year;

   days = daysYear(year);
   cout << "Number of days: " << days;

   return 0;
}

Last edited on
The program above compiles but it doesn't give the right out put and I am not sure why. I really don't know what I'm doing here so please help.
What does it output?
So this is it now and its off by one. For some reason in 2004 and 2008 its off by one and I am not sure why.
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

int isLeapYear(int yearPassedFromDaysYear)
{
   if (( yearPassedFromDaysYear % 400 ) == 0)
   {
      return(366);
   }else if (( yearPassedFromDaysYear % 100) == 0)
   {
      return(365);
   }else if (( yearPassedFromDaysYear % 4) == 0)
   {
      return(366);
   }else
   {
      return(365);
   }
}

int daysYear(int yearPassedFromMain)
{
   int holderV = 0;

   while ((yearPassedFromMain - 1753) > 0)
   {
      holderV = holderV + isLeapYear(yearPassedFromMain);
      yearPassedFromMain --;
   }

   return holderV;
}

int main()
{
   int year = 0;
   int days = 0;

   cout << "Year: ";
   cin >> year;

   days = daysYear(year);
   cout << "Number of days: " << days;

   return 0;
}
I thought it would be clear, sorry: I renamed your daysYear() function to daysSince1753() because it seemed more fitting.

Anyway, take a good look at your program above and think for a while about what it does. You should be able to explain what each line of code does. When in doubt, go back to your book or this site's tutorial until you understand what you're doing. Your code never calls daysSince1753(), for instance, and it adds a bool to holderV. A bool will be converted to 0 or 1 if used as you do. Basically, your code is simply adding 1 to holderV every time isLeapYear() returns true. You're probably ending up with the number of leap years since 1753. Also, the expression on line 27 (-1) isn't really doing anything, although it is legal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cout;
bool IsLeapYear( int );

int main()
{
    for(int i = 2008; i < 2015; i++){
        if (IsLeapYear(i)){
            cout << i << " is Leap Year!!!\n";
        }
        else{
            cout << "I guess " << i << " is not leap year...\n";
        }
    }
    
    return 0;
}

bool IsLeapYear( int year ){
    return (year % 4 == 0)?true:false;
}



2008 is Leap Year!!!
I guess 2009 is not leap year...
I guess 2010 is not leap year...
I guess 2011 is not leap year...
2012 is Leap Year!!!
I guess 2013 is not leap year...
I guess 2014 is not leap year...
Topic archived. No new replies allowed.