Starting with
obj2.C
,
kheslin2/three.C
declared the distance
function but forgot to define it.
Ditto for the constructor with no explicit arguments.
Two variables in
main
with the same name (t
).
ak205/three.C
thinks that the distance from December 31, 2024 to January 1, 2025
is 366 days.
It should be 1 day.
We will handle leap year later in the course when we do
inheritance and polymorphism.
The code to test if the year
i
is a leap year can be simplified from
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) {to
if (i % 400 == 0 || i % 100 != 0 && i % 4 == 0) {but this doesn’t fix the bug.
jr224/three.C
thinks that the distance from January 1, 2025 to January 1, 2026
is 0 days.
It should be 365 days.
For example, today (February 13, 2025) is day number 31 + 13 = 44 of the year.
// Return what day of the year (1 to 365 inclusive) this date object is. int date::dayOfYear() const { int sum {0}; for (int i {1}; i < month; ++i) { sum += length[i]; } sum += day; return sum; }
// Return what day of the year (1 to 365 inclusive) this date object is. int date::dayOfYear() const { int sum {day}; for (int i {1}; i < month; ++i) { sum += length[i]; } return sum; }
Remember,
length
means
&length[0]
length + 1
means
&length[1]
length + month
means
&length[month]
This call to the
accumulate
function returns an
int
is because it’s looping through an array of
int
s.
#include <algorithm> //for the accumulate function using namespace std; // Return what day of the year (1 to 365 inclusive) this date object is. int date::dayOfYear() const { return accumulate(length + 1, length + month, day); }