none
A program which works out how many days the user has been alive RRS feed

  • Question

  • I would like someone to help me to write a program which tells the user how many days theyve been aliva after they iput thir birthday.
    Sunday, September 21, 2014 12:34 PM

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 (01-12):")
    monthBirthday = TextWindow.ReadNumber()
    TextWindow.WriteLine("Please enter the day of your birth (01-31):")
    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 " + (TotalDays-1) + " day(s)")

    Sunday, September 21, 2014 3:57 PM
  • 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!"


    Wednesday, September 24, 2014 5:16 AM

All replies

  • What code do you have so far, are you using extensions or not?

    Did you see this recent post?

    http://social.msdn.microsoft.com/Forums/en-US/772ff32e-fb09-4245-9c10-cde773277f27/how-many-days-until-youre-100?forum=smallbasic

    Sunday, September 21, 2014 2:45 PM
    Moderator
  • 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 (01-12):")
    monthBirthday = TextWindow.ReadNumber()
    TextWindow.WriteLine("Please enter the day of your birth (01-31):")
    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 " + (TotalDays-1) + " day(s)")

    Sunday, September 21, 2014 3:57 PM
  • cnk_gr, good sample.

    But, we need to check 400 for leap year.  Because year 2000 was a leap year.


    Nonki Takahashi


    Wednesday, September 24, 2014 4:08 AM
    Moderator
  • 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!"


    Wednesday, September 24, 2014 5:16 AM