counting the number of leapyears

So I am working on a calendar project. I had a lot written out initially, but since I had a lot of errors, I decided to work on it piece by piece. My goal right now is calculate the number of leap years. I want to do this because some years will have 366 and some will have 365. By determining how many days has past, I can determine when the first day is supposed to start in the month. Here is my code that I have so far. Main really isn't supposed to call the number of leap years, but I just have it there to test it, and I'm constantly getting 0 as an output.

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
65
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;
}

  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 (getYear(yearNumber) % 400 == 0 || getYear(yearNumber)
           % 100 == 0 && getYear(yearNumber % 4 == 0));
}
/**********************************************************************
 * This function will loop and count every leap year.
 ***********************************************************************/
int totalLeapYears(int yearNumber)
{
   int leapYears = 0;

   for (int i = 0; i <= yearNumber - 1753; i++)
      if (isLeapYear(i))
      {
         leapYears++;
            }
      return leapYears;
}
int main()
{
   int monthNumber;
   int yearNumber;
   int leapYears;

   getMonth(monthNumber);
   getYear(yearNumber);
   cout <<  totalLeapYears(yearNumber) << endl;
   return 0;
}


In getMonth and getYear you are confused as to whether you are returning the value as an argument (in which case you need to make it a reference parameter with a &) or as the value of the function (in which case you should change the calls in main()) - I suggest the latter.

Your logic for a leap year is wrong and seems to keep asking for the year again and again.

Your loop index should be going through the years, not starting at 0.

Please put all include statements in your code so that we can test it in cpp shell.


I assume 1753 is significant because they knocked 10 days out of the calendar the year before (in Britain, anyway).


Try:
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
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
using namespace std;


int getMonth()                         // <===== return value of function, not argument
{
   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;
}


int getYear()                          // <===== return value of function, not argument
{
   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 )                           // <==== logic was all over the place: simplify
{
   return (      yearNumber % 4   == 0                                  // every fourth year usually a leap year
            && ( yearNumber % 100 != 0 || yearNumber % 400 == 0 ) );    // except end of century, unless a fourth century
}

/**********************************************************************
 * This function will loop and count every leap year since 1753
 ***********************************************************************/
int totalLeapYears( int yearNumber )
{
   int leapYears = 0;

   for ( int i = 1753; i <= yearNumber; i++ )               // <==== loop tests didn't reflect year
   {
      if ( isLeapYear( i ) ) leapYears++;
   }
   return leapYears;
}


int main()
{
   int monthNumber;
   int yearNumber;
   int leapYears;

   monthNumber = getMonth();                                // <==== values returned from function
   yearNumber = getYear();                                  //
   cout <<  totalLeapYears( yearNumber ) << endl;
   return 0;
}





Last edited on
Thanks so much for the help, sorry I forgot the include in statements.
Topic archived. No new replies allowed.