Answered by:
A program which works out how many days the user has been alive
Question

Answers

Without range checking I think this is correct:
TextWindow.WriteLine("Please enter the year of your birth (yyyy):") yearBirthday = TextWindow.ReadNumber() TextWindow.WriteLine("Please enter the month of your birth (0112):") monthBirthday = TextWindow.ReadNumber() TextWindow.WriteLine("Please enter the day of your birth (0131):") dayBirthday = TextWindow.ReadNumber() TotalDays = 0 For curYear = yearBirthday To Clock.Year If curYear = yearBirthday Then FirstMonth = monthBirthday Else FirstMonth = 1 EndIf If curYear = Clock.Year Then LastMonth = Clock.Month Else LastMonth = 12 EndIf For curMonth = FirstMonth To Lastmonth If curMonth = 1 Or curMonth = 3 Or curMonth = 5 Or curMonth = 7 Or curMonth = 8 Or curMonth = 10 Or curMonth = 12 then MaxDays = 31 EndIf If curMonth = 4 or curmonth = 6 or curmonth = 9 or curmonth = 11 Then MaxDays = 30 endif If curMonth = 2 Then MaxDays = 28 If (math.Remainder(curYear,4) = 0) And (math.Remainder(curYear,100) <> 0) Then MaxDays = 29 endif endif If (Clock.year = curyear) and (clock.month = curmonth) Then MinDays = dayBirthday Else MinDays = 1 endif If (Clock.Year = curyear) And (clock.month = curmonth) Then MaxDays = Clock.Day ENDIF For curDay = MinDays to MaxDays TotalDays = TotalDays + 1 endfor EndFor EndFor TextWindow.WriteLine("Your age is " + (TotalDays1) + " day(s)")
 Proposed as answer by Nonki TakahashiModerator Wednesday, September 24, 2014 7:37 AM
 Marked as answer by Nonki TakahashiModerator Wednesday, October 1, 2014 4:39 AM

Then change to:
If (math.Remainder(curYear,4) = 0) And ((math.Remainder(curYear,100) <> 0) or (math.Remainder(curYear,400) = 0)) Then MaxDays = 29 endif
For better performance you can also change this:
For curDay = MinDays to MaxDays TotalDays = TotalDays + 1 endfor
to this:
If MaxDays >= MinDays Then TotalDays = TotalDays + MaxDays  MinDays + 1 EndIf
I haven't thoroughly tested the logic, and to be fair it's my first try on Small Basic but I think it's ok. I may be off by a day but I can't test this right now so I'll get back on this later.
"If there's nothing wrong with me, maybe there's something wrong with the universe!"
 Edited by cnk_gr Wednesday, September 24, 2014 5:16 AM
 Proposed as answer by Nonki TakahashiModerator Wednesday, September 24, 2014 7:38 AM
 Marked as answer by Nonki TakahashiModerator Wednesday, October 1, 2014 4:39 AM
All replies

What code do you have so far, are you using extensions or not?
Did you see this recent post?

Without range checking I think this is correct:
TextWindow.WriteLine("Please enter the year of your birth (yyyy):") yearBirthday = TextWindow.ReadNumber() TextWindow.WriteLine("Please enter the month of your birth (0112):") monthBirthday = TextWindow.ReadNumber() TextWindow.WriteLine("Please enter the day of your birth (0131):") dayBirthday = TextWindow.ReadNumber() TotalDays = 0 For curYear = yearBirthday To Clock.Year If curYear = yearBirthday Then FirstMonth = monthBirthday Else FirstMonth = 1 EndIf If curYear = Clock.Year Then LastMonth = Clock.Month Else LastMonth = 12 EndIf For curMonth = FirstMonth To Lastmonth If curMonth = 1 Or curMonth = 3 Or curMonth = 5 Or curMonth = 7 Or curMonth = 8 Or curMonth = 10 Or curMonth = 12 then MaxDays = 31 EndIf If curMonth = 4 or curmonth = 6 or curmonth = 9 or curmonth = 11 Then MaxDays = 30 endif If curMonth = 2 Then MaxDays = 28 If (math.Remainder(curYear,4) = 0) And (math.Remainder(curYear,100) <> 0) Then MaxDays = 29 endif endif If (Clock.year = curyear) and (clock.month = curmonth) Then MinDays = dayBirthday Else MinDays = 1 endif If (Clock.Year = curyear) And (clock.month = curmonth) Then MaxDays = Clock.Day ENDIF For curDay = MinDays to MaxDays TotalDays = TotalDays + 1 endfor EndFor EndFor TextWindow.WriteLine("Your age is " + (TotalDays1) + " day(s)")
 Proposed as answer by Nonki TakahashiModerator Wednesday, September 24, 2014 7:37 AM
 Marked as answer by Nonki TakahashiModerator Wednesday, October 1, 2014 4:39 AM

cnk_gr, good sample.
But, we need to check 400 for leap year. Because year 2000 was a leap year.
Nonki Takahashi
 Edited by Nonki TakahashiModerator Wednesday, September 24, 2014 4:09 AM typo

Then change to:
If (math.Remainder(curYear,4) = 0) And ((math.Remainder(curYear,100) <> 0) or (math.Remainder(curYear,400) = 0)) Then MaxDays = 29 endif
For better performance you can also change this:
For curDay = MinDays to MaxDays TotalDays = TotalDays + 1 endfor
to this:
If MaxDays >= MinDays Then TotalDays = TotalDays + MaxDays  MinDays + 1 EndIf
I haven't thoroughly tested the logic, and to be fair it's my first try on Small Basic but I think it's ok. I may be off by a day but I can't test this right now so I'll get back on this later.
"If there's nothing wrong with me, maybe there's something wrong with the universe!"
 Edited by cnk_gr Wednesday, September 24, 2014 5:16 AM
 Proposed as answer by Nonki TakahashiModerator Wednesday, September 24, 2014 7:38 AM
 Marked as answer by Nonki TakahashiModerator Wednesday, October 1, 2014 4:39 AM