total of days in previous month

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.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <iomanip>
using namespace 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;

   else if (monthNumber == 2) //days in current year previous to Feb.
      return 31;

   else if (monthNumber == 3) //days in current year previous to Mar.
      return 31 + 28; if (isLeapYear(yearNumber)) return 31 + 29;

   else if (monthNumber == 4) //days in current year previous to Apr.
      return 31 + 28 + 31; if (isLeapYear(yearNumber)) return 31 + 29 + 31;

   else if (monthNumber == 5) //days in current year prevous to May
      return 31 + 28 + 31 + 30; if (isLeapYear(yearNumber))
                                   return 31 + 29 + 31 + 30;

   else if (monthNumber == 6) //days in current year previous to Jun.
      return 31 + 28 + 31 + 30 + 31;
    if (isLeapYear(yearNumber))
      return 31 + 29 + 31 + 30 + 31;

   else if (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;

   else if (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;

   else if (monthNumber == 9) //days in current year previous to Sep.
      return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
  else if (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;

   else if (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;

   else if (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;
}

Last edited on
Lines 87-130: All of your if statements involving monthNumber are using the assignment operator (=), not the comparison operator (==).
thanks so much! I should have tried first, I always get == and = confused.
Topic archived. No new replies allowed.