SwolleyMammmoth
SwolleyMammmoth

Reputation: 25

Can't Get Output Right in Visual Basic Tax Calculator

I have a test coming up so I'm trying to do as many problems in the book as possible before it, but I can't figure out why I'm getting "c" as an output in my windows form app from this code. If anyone can help I'd appreciate it.

    Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles 
btnCompute.Click
        Dim income, tax As Double

        income = txtIncome.Text

        Select Case income
        Case Is > 9225
            tax = income * 0.1
        Case Is > 37450
            tax = 922.5 + 0.15 * (income - 9225)
        Case Is > 90750
            tax = 5617.5 + 0.25 * (income - 37450)
        Case Is > 189300
            tax = 22687.5 + 0.28 * (income - 90750)
        Case Is > 411500
            tax = 53004 + 0.33 * (income - 189300)
        Case Is > 413200
            tax = 135795 + 0.35 * (income - 411500)
        Case Is < 413200
            tax = 144620 + 0.396 * (income - 413200)
    End Select

    txtTax.Text = String.Format("{0: C}", income)
End Sub

Upvotes: 0

Views: 197

Answers (2)

Patrick
Patrick

Reputation: 196

The only case statements that can hit as written are > 9225 and < 413200 (not sure why that is even there). You need to flip the order of the case statements.

Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
    Dim income, tax As Double

    income = txtIncome.Text

    Select Case income
        Case Is > 413200
            tax = 144620 + 0.396 * (income - 413200)
        Case Is > 411500
            tax = 135795 + 0.35 * (income - 411500)
        Case Is > 189300
            tax = 53004 + 0.33 * (income - 189300)
        Case Is > 90750
            tax = 22687.5 + 0.28 * (income - 90750)
        Case Is > 37450
            tax = 5617.5 + 0.25 * (income - 37450)
        Case Is > 9225
            tax = 922.5 + 0.15 * (income - 9225)
        Case Else
            tax = 0.1 * income
    End Select

    txtTax.Text = String.Format("{0:C}", tax)
End Sub

Also, it looks like you are using 2015 tax tables. Your constants aren't adding up. (May be my fault).

Upvotes: 1

vladatr
vladatr

Reputation: 626

Maybe with no space:

 txtTax.Text = String.Format("{0:C}", tax)

Upvotes: 3

Related Questions