A slightly less advanced question...

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
 
J

jbruno4000

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 think it would be much easier for you if you were to convert the dates to
Julian days before doing your calculations. i.e.

If birthdate ends up being 1566 and current day ends up being 2015, of course
you'd have to make an adjustment for the leap years in between, but you'd end
up with a much easier algorithm.

JB
 
L

lilburne

Tom said:
Could someone please give me some hints, corrections or pointers? I'd be
indebted...

Simple solution:

Assume DOB is current year, how many days from start of year to
birthday, how many days from start of year to current date. Difference
is number of days since/before birthday (TO_BD). How many years alive?
Add 365 or 366 for each year depending on whether feb has 28 days in
that year (YEAR_DAYS). result = YEAR_DAYS + TO_BD.

You may have to tweak the signs slighly.
 
K

Karl Heinz Buchegger

Tom said:
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.

OK.
So the next thing you should do:
familiarize yourself with the debugger. A debugger is a program which 'runs'
your program. The point is: In doing so, the debugger offers facilities
to eg.
watch variables as they change values

'single step' your program. That is execute the next statement in your program
You most probably want to check the involved variables, if they indeed contain
the valus you expect.

set breakpoints: That is when the point of execution reaches a statement, the
debugger kicks in, interupts your program and again allows you to have a look
at variables and what values they contain.

... (much, much more. But the above should convince you that knowing how to use
your debugger is a valuable thing to learn),

So now you may ask: What if I don't have a debugger.
In this case, do it the old fashioned way: Insert output statements which show
you:
* what execution paths your program has taken
* which values some interesting variables have.


eg. you have a problem in the following function:
int diffTwoDates(Datum aDate, Datum anotherDate)

{
int diff = 0; // to keep track of the difference in days

std::cout << "Calculating difference of " << aDate._day << " "
<< aDate._month << " "
<< aDate._year << std::endl;
std::cout << and " << anotherDate._day << " "
<< anotherDate._month << " "
<< anotherDate._year << std::endl;
do {
for (aDate._day; aDate._day < daysInMonth(aDate._year,
aDate._month); aDate._day++)
// count up from starting day until end of the month
{
diff++;
}

cout << "Counting up to " << diff << "for month " << aDate._month << "/" << aDate._year << endl;
if (aDate._day == daysInMonth(aDate._year, aDate._month))
// day has reached the end of the month

Rewritten:

int MonthDays = daysInMonth(aDate._year, aDate._month);

cout << "There are " << MonthDays << " days in " << aDate._month << "/" << aDate._year << endl;

if( aDate._day == MonthDays )

{
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

cout << "Month was december!" << endl;
cout << "New year: " << aDate._year << " New month " << aDate._month << endl;
}
else
{
aDate._month++;

cout << "New month " << aDate._month << endl;
}
// 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
}

cout << "bottom of loop reached, comparing dates" << endl;
} while (!(aDate._day != anotherDate._day && aDate._month !=
anotherDate._month && aDate._year != anotherDate._year));

cout << "While loop finished, diff = " << diff << endl;
return diff;
}
[snip]

So much for some simple debugging techniques.
Could someone please give me some hints, corrections or pointers? I'd be
indebted...

Insert the output statements and that watch your programs output. It will tell
you in which order which operations were performed and what results were reached.
If you have problems in one specific area: insert additional output statements.
Do this until you know exactly what's going on. Analyzing this output will
eventually bring you in a situation where you clap your hands at your forehead
and yelling: "My god, how could I have missed that!"
Then you fix your problem, watch the output one more time, convice yourself
that it now produces the correct result and simply comment away the additional
output statements (You will need them again, if it turns out that there is
one additional bug left and the whole procedure starts afresh.
 
C

Chris Dams

Hello,

Tom said:
} while (!(aDate._day != anotherDate._day && aDate._month !=
anotherDate._month && aDate._year != anotherDate._year));

This condition is wrong. A little debugging could have told you that.
Perhaps this isn't the most elegant code one could write, but it's the

No, it is also rather inefficient.

Bye,
Chris Dams
 
T

Tom

[Friday 28 November 2003 15:09] jbruno4000 in comp.lang.c++:

<snip>

Thanks to all of you; especially Karl for taking the time to write a
(very) small essay. I'll see how far I can get.

Greets,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top