By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

A slightly less advanced question...

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
>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
Jul 22 '05 #2

P: n/a
Hello,

Tom <tom @ abwaerts . be> writes:
} 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
Jul 22 '05 #3

P: n/a


Tom wrote:

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.
Jul 22 '05 #4

P: n/a
Tom wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

P: n/a
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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.