Compute a person's age today based on his date of birth. - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Compute a person's age today based on his date of birth. (/thread-7498.html) |
Compute a person's age today based on his date of birth. - Moneo - 06-09-2005 INPUT: The person's date of birth as YYYYMMDD. LOGIC: - Validate the input date of birth. Abort if invalid. - Get today's date using the DATE$ function. - Compute the person's age in years using a calculation method, or if you prefer perform some sort of loop to get to it. OUTPUT: The person's age in years. Note: If his birthday happens to be today, he will be one year older (obviously). ***** Compute a person's age today based on his date of birth. - Neo - 06-10-2005 Sorry for the wait... I was quite busy lately. I hope this produces some answers that are correct. I tested it a few times and it returned right ages for all my family members Code: Option Static NOTE: FreeBasic code I hope it works and suits your challenge objectives. Compute a person's age today based on his date of birth. - ThaMariuZ - 06-10-2005 woah, the code is very long... i don`t have any time at the moment, but 'm gonna code a qb prog as soon as i get to. cheers Compute a person's age today based on his date of birth. - Neo - 06-10-2005 It's not very long... half the code is actually checking if the input birthday is a valid date (note that people who are yet to be born have age 0). The actual "calculation"... it's more a loop, happens in the getPersonAge function, which isn't very long Compute a person's age today based on his date of birth. - Moneo - 06-10-2005 Neo, I get the following FB compile error: error 64: Too many expressions, found: 'Not' On Line: 13 which is: Dim Datum As String, isOk As Byte = Not( 0 ), i As Integer Reviewing your code, I too feel that it is too long. The long function "getpersonAge" can be done in about 5 instructions without any loop. If the person is less than a year old, like a 6 month old baby, his age is zero years. If the person was not born yet, then he has no date of birth. You did a nice job of validating the input date of birth. Fix the compile error and I'll do some testing of this version. ***** Compute a person's age today based on his date of birth. - Neo - 06-10-2005 The problem is there is no compile error Maybe download the latest FB version? I have no compiler error at all with the latest FB IDE and FB version. And yes, I just did the loop because it's easier, else you'd have to do all checks with date shiftings. NOTE: My code treats a leapling as a leapling, which means, he'll only get a birthday every leapyear! (officially this is the case). Compute a person's age today based on his date of birth. - Moneo - 06-10-2005 Quote:The problem is there is no compile errorSorry, Neo, I was using the previous version of FB. I have problems with the lastest version of FBIDE. It issues an error saying it can't find "\ide\spash.png". I ignored the error and continued to compile your program. It works fine. I don't know what you mean by: "... else you'd have to do all checks with date shiftings." But let's leave this subject until after I post my solution, or someone else does it the same way. Ok? About the leapling. I assume you mean a person born on a leap year on February 29th. You say that "officially" he has a birthday every leapyear. That may be aesthetically true, but the goverment of most any nation will consider his age as a multiple of years, just like anybody else, for reasons of having to sign up for military service, registering to vote, paying taxes, getting a drivers license, etc. ***** Compute a person's age today based on his date of birth. - Deleter - 06-10-2005 well, its just about as elementary as you can get, but hey w/e. this was coded in fb (hence the sleep at the end): Code: birth$ = "20040101" Compute a person's age today based on his date of birth. - Moneo - 06-10-2005 Deleter, I compiled your solution and ran a few tests using valid data, and it ran ok. I have the following comments which I hope you take in a constructive manner: 1) Minor point: You did not input the date of birth, you inserted one into a variable. 2) You did not perform any validation on the input date of birth. No wonder the problem became elementary. 3) In obtaining the current date using DATE$, you invoked the DATE$ function three separate times. This could rarely happen, but if you were running the program just at midnight, then you could get two different dates, before midnight and after midnight, which would affect your final year, month, day. If you'd like, you can fix up your program and submit it again. ***** Compute a person's age today based on his date of birth. - Neo - 06-10-2005 Moneo, in an attempt to shorten the getPersonAge function I came up with the following, bizarre result. I know it shouldn't be working fully but I haven't found a current date / birth date yet for which it doesn't work... and I tried around 400 times. I hoped you could test the functionality of both my submitted functions Code: Private Function getPersonAge (Year As Integer, Month As Integer, Day As Integer, _ Note, to test this function you replace the old getPersonAge function by this 3-line new function. Hmmm... 600 tests... no difference yet... |