1. KBartos's Avatar
    Write a program in Visual Basic 6.0 according to the following specifications:
    Description
    The program first asks the user for the month and year of his/her birth.
    Then the program validates the input if it's within the allowed range for months and years, and prompts the user in case of invalid input.
    If the input is correct the program then computes the age of the person and:
    if it is over 65 it prints "You are a senior".
    if it is under 18 it prints "You are a kid"
    otherwise (if age is equal or between 18 and 65) it prints "You are an adult".
    In addition, if the birth month is the same as the current month, then the program also prints "Happy Birthday".
    Implementation
    Use two separate Text Boxes or Input Boxes for input and print on a Picture Box. Hints: For computing the age use code from Lecture 3, Problem 2. Then include If-Then or If-Then-Else blocks that check for the range of variable Years. Add also an If block that compares MOB with CurrentMonth and prints the birthday message.
    Documentation
    Run your program and make sure it works as described above. Then add your name in the beginning of the code as a comment (with Rem or '). Add also comments to explain how the code works. Note that comments and the way you format your program (follow the program format used in the book) will be graded too.




    Heres the codes we got to use from Lecture 3, problem 2. Can paypal some $ for help

    Private Sub Form_Click()
    Dim MOB As Integer, YOB As Integer, Months As Integer, Years As Integer, MonthsLived As Integer
    Const CurrentMonth As Integer = 2
    Const CurrentYear As Integer = 2009
    MOB = InputBox("Enter Month of Birth")
    YOB = InputBox("Enter Year of Birth")
    MonthsLived = CurrentMonth + CurrentYear * 12 - MOB - YOB * 12
    Months = MonthsLived Mod 12 ' Remainder from integer division
    Years = MonthsLived \ 12 ' Integer division
    Cls
    Print " You are "; Years; "years and "; Months; " months old"
    End Sub

    Solution 2: Data validation (see Top 25 Most Dangerous Programming Errors)

    Private Sub Form_Click()
    Dim MOB As Integer, YOB As Integer, Months As Integer, Years As Integer, MonthsLived As Integer
    Const CurrentMonth As Integer = 2
    Const CurrentYear As Integer = 2009
    MOB = InputBox("Enter Month of Birth")
    YOB = InputBox("Enter Year of Birth")
    If YOB > 2009 Or YOB < 1900 Then ' Check if YOB is out of the interval [1900,2009]
    Print YOB, "Wrong year"
    Else
    MonthsLived = CurrentMonth + CurrentYear * 12 - MOB - YOB * 12
    Months = MonthsLived Mod 12 ' Remainder from integer division
    Years = MonthsLived \ 12 ' Integer division
    Cls
    Print " You are "; Years; "years and "; Months; " months old"
    End If
    End Sub

    Solution 3: More elaborated verification - ElseIf clause.

    Private Sub Form_Click()
    Dim MOB As Integer, YOB As Integer, Months As Integer, Years As Integer, MonthsLived As Integer
    Const CurrentMonth As Integer = 2
    Const CurrentYear As Integer = 2009
    MOB = InputBox("Enter Month of Birth")
    YOB = InputBox("Enter Year of Birth")
    If YOB > 2009 Or YOB < 1900 Then
    Print YOB, "Wrong year"
    ElseIf MOB > 12 Or MOB < 1 Then
    Print MOB, "Wrong month"
    Else
    MonthsLived = CurrentMonth + CurrentYear * 12 - MOB - YOB * 12
    Months = MonthsLived Mod 12 ' Remainder from integer division
    Years = MonthsLived \ 12 ' Integer division
    Form1.Cls
    Print " You are "; Years; "years and "; Months; " months old"
    End If
    End Sub

    My teacher speaks no English, my GFs brother is taking the class with another professor and I asked him for some help he said " IDK what that is were not that far yet" FML
    02-18-09 05:11 PM
  2. elpresidente408's Avatar
    It's fairly straightforward. You have the bulk of the code already, you'd just need a couple of if statements. Give it a shot first.
    02-18-09 05:32 PM
  3. KBartos's Avatar
    It's fairly straightforward. You have the bulk of the code already, you'd just need a couple of if statements. Give it a shot first.
    I've tried. Cant figure it out for the life of me. We haven't covered anything in class like it.
    02-18-09 06:00 PM
  4. elpresidente408's Avatar
    Here you go. I made the answers pop up in a message box. You'd have to change them if you wanted it to appear in a text box.

    Private Sub Button1_Click()
    Dim MOB As Integer
    Dim YOB As Integer

    MOB = InputBox("Enter your month of birth")
    If (MOB > 12) Or (MOB < 1) Then
    MsgBox("Please enter a valid month")
    Exit Sub
    End If

    YOB = InputBox("Enter your year of birth")
    If (YOB > 2009) Or (YOB < 1900) Then
    MsgBox("Please enter a valid year")
    Exit Sub
    End If

    If ((2009 - YOB) > 65) And (MOB >= 2) Then
    MsgBox("You are a senior")
    ElseIf ((2009 - YOB) < 18) And (MOB < 2) Then
    MsgBox("You are a child")
    Else
    MsgBox("You are an adult")
    End If

    If MOB = 2 Then
    MsgBox("Happy Birthday!")
    End If
    End Sub
    02-18-09 10:40 PM
LINK TO POST COPIED TO CLIPBOARD