lxtrxi
lxtrxi

Reputation: 55

Formatting percentages more thorougly

I've been asked to make a program to find percentages of males, females and children, but I'm kind of stuck - it's hard to explain, but when you enter an age that's younger than or equal to 17 it would be a child and anything over would be a male or female. However when it's entered it counts it as two people instead of one because it asks for the gender after the age if it's younger than 17. If it's hard to explain, please let me know and I'll try to improve on how I'm telling it.

Current code:

Public Class Form1

Dim MalePercent, FemalePercent, ChildPercent As Single
Dim MaleCount, FemaleCount, ChildCount, CountTotal, Age As Integer
Dim Gender As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim buttonArray = {btnMale, btnFemale}
    For Each button In buttonArray
        button.Enabled = False
    Next
    MessageBox.Show("To start this program, please click a button of your choice.")
    txtAge.Select()
    txtAge.Clear()
End Sub

Private Sub btnMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMale.Click
    MaleCount += 1
    CountTotal += 1
    lblMaleCount.Text = MaleCount
    lblCountTotal.Text = CountTotal
    btnMale.Enabled = False
    btnFemale.Enabled = False
    txtAge.Enabled = True
    txtAge.Select()
    txtAge.Clear()
End Sub

Private Sub btnFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFemale.Click
    FemaleCount += 1
    CountTotal += 1
    lblFemaleCount.Text = FemaleCount
    lblCountTotal.Text = CountTotal
    btnFemale.Enabled = False
    btnMale.Enabled = False
    txtAge.Enabled = True
    txtAge.Select()
    txtAge.Clear()
End Sub

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    MalePercent = MaleCount / CountTotal
    lblMalePercent.Text = Format(MalePercent, "#0.00%")

    FemalePercent = FemaleCount / CountTotal
    lblFemalePercent.Text = Format(FemalePercent, "#0.00%")

    ChildPercent = ChildCount / CountTotal
    lblChildPercent.Text = Format(ChildPercent, "#0.00%")
End Sub

Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
    MessageBox.Show("Thank-you for using this program. Good-bye!")
    Close()
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    Age = txtAge.Text
    If Age <= 17 Then
        MessageBox.Show("Now please select the appropriate gender.")
        ChildCount += 1
        CountTotal += 1
        lblChildCount.Text = ChildCount
        lblCountTotal.Text = CountTotal

    ElseIf Age >= 18 Then
        MessageBox.Show("Now please select the appropriate gender.")
    End If
        txtAge.Enabled = False
        Dim buttonArray = {btnMale, btnFemale}
        For Each button In buttonArray
            button.Enabled = True
        Next
End Sub    

Upvotes: 0

Views: 48

Answers (1)

Chase Rocker
Chase Rocker

Reputation: 1908

Looks like you're incrementing CountTotal twice when it's a Child, once then the age is entered and again when a gender is chosen.

Try this:

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    Age = txtAge.Text

    If Age <= 17 Then
        ChildCount += 1
        'CountTotal += 1  <= remove this
        lblChildCount.Text = ChildCount
        'lblCountTotal.Text = CountTotal <= remove this
    End If

        'messagebox here because you always ask for gender no matter the age
        MessageBox.Show("Now please select the appropriate gender.")

        txtAge.Enabled = False
        Dim buttonArray = {btnMale, btnFemale}
        For Each button In buttonArray
            button.Enabled = True
        Next
End Sub    

Upvotes: 2

Related Questions