473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A slightly less advanced question...

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
5 1460
>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
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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

375
by: rkusenet | last post by:
This article is very bleak about future of DB2. How credible is the author. http://www.eweek.com/article2/0,1895,1839681,00.asp
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
3
by: jrhoads23 | last post by:
If you look at a standard Button in a .NET Windows Forms app, you will notice its default BackColor is "Control" and it has a 3D raised border which is 2 pixels wide. The outer edge on the left and...
4
by: z. f. | last post by:
Hi, i stated that this is an advanced question because i have a post from few days ago that i received answers to with suggestions that looked good but did not work, so please if you post a...
68
by: pemo | last post by:
What would expect to be covered? Say you'd already attended a course, that had covered stuff like structs, pointers, bitwise and logical operators, multi-demensional arrays , *but* hadn't covered...
7
by: ashu | last post by:
i m relatively new to this group. i just want to know, is there any site which can provide any help on advanced C topics such as machine interaction through C, memory management through C etc
27
by: Smithers | last post by:
Until now I have worked on small teams (1-3 developers) and we've been able to stay out of each others way. Now I'm about to start work on a project that will have 5 developers. I would appreciate...
1
by: Parv | last post by:
I am trying to impersonate user to some other system using userName,domainName,password in C#. My Code is working fine if i am working on Windows 2000 professional after assigning current user "Act...
0
by: kishjeff | last post by:
Hi. This is a slightly convoluted question, but maybe it is obvious.. <convolutedquestion> I'd like to search a set of xml documents and get the element name and its id attribute of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.