T
Tom
Hey,
I'm quite new to C++ (or to programming as a whole, for that matter). My
courses started in september; last thing we saw were functions and
classes.
I can't seem to get the assignment for this week right. The second part
of it prompts a user for the current date and his/her birthday; after
that, the difference should be calculated as a number of days.
I have a function that determines whether a year is a leap year or not.
With it, the number of days in a certain month of a certain year can be
calculated (daysInMonth). So the last function should be able to count
up from the birthday to the present date, keeping track of the
difference. Although I wrote at least something my tiny brain thinks is
okay, the little program doesn't work correctly, in that it only count
up until the end of the month of the initial date. The function has two
instances of a tiny class as parameters (the class "Datum" just
contains three integers, "day", "month" and "year"), which might
already be blatantly wrong... :-s
What I have is this:
int diffTwoDates(Datum aDate, Datum anotherDate)
{
int diff = 0; // to keep track of the difference in days
do {
for (aDate._day; aDate._day < daysInMonth(aDate._year,
aDate._month); aDate._day++)
// count up from starting day until end of the month
{
diff++;
}
if (aDate._day == daysInMonth(aDate._year, aDate._month))
// day has reached the end of the month
{
if (aDate._month == 12)
{
aDate._year++;
aDate._month = 1;
// if that month happened to be December, the year
// goes one up, and the month starts at 1 again
}
else
{
aDate._month++;
}
// if it's another month, just the month goes one up
aDate._day = 1;
// in both cases, date needs to go up one, to keep the
// difference counter of the for loop going
}
} while (!(aDate._day != anotherDate._day && aDate._month !=
anotherDate._month && aDate._year != anotherDate._year));
return diff;
}
Perhaps this isn't the most elegant code one could write, but it's the
best I could come up with. Since the program compiles just fine (and
works at least partly), I guess it's not a matter of syntax, but of
logic (which gives me the ominous feeling I'm not made for this,
although I really like it... :-/).
Could someone please give me some hints, corrections or pointers? I'd be
indebted...
Greets,
Tom
I'm quite new to C++ (or to programming as a whole, for that matter). My
courses started in september; last thing we saw were functions and
classes.
I can't seem to get the assignment for this week right. The second part
of it prompts a user for the current date and his/her birthday; after
that, the difference should be calculated as a number of days.
I have a function that determines whether a year is a leap year or not.
With it, the number of days in a certain month of a certain year can be
calculated (daysInMonth). So the last function should be able to count
up from the birthday to the present date, keeping track of the
difference. Although I wrote at least something my tiny brain thinks is
okay, the little program doesn't work correctly, in that it only count
up until the end of the month of the initial date. The function has two
instances of a tiny class as parameters (the class "Datum" just
contains three integers, "day", "month" and "year"), which might
already be blatantly wrong... :-s
What I have is this:
int diffTwoDates(Datum aDate, Datum anotherDate)
{
int diff = 0; // to keep track of the difference in days
do {
for (aDate._day; aDate._day < daysInMonth(aDate._year,
aDate._month); aDate._day++)
// count up from starting day until end of the month
{
diff++;
}
if (aDate._day == daysInMonth(aDate._year, aDate._month))
// day has reached the end of the month
{
if (aDate._month == 12)
{
aDate._year++;
aDate._month = 1;
// if that month happened to be December, the year
// goes one up, and the month starts at 1 again
}
else
{
aDate._month++;
}
// if it's another month, just the month goes one up
aDate._day = 1;
// in both cases, date needs to go up one, to keep the
// difference counter of the for loop going
}
} while (!(aDate._day != anotherDate._day && aDate._month !=
anotherDate._month && aDate._year != anotherDate._year));
return diff;
}
Perhaps this isn't the most elegant code one could write, but it's the
best I could come up with. Since the program compiles just fine (and
works at least partly), I guess it's not a matter of syntax, but of
logic (which gives me the ominous feeling I'm not made for this,
although I really like it... :-/).
Could someone please give me some hints, corrections or pointers? I'd be
indebted...
Greets,
Tom