I need help with the calculations done in a change concevter. The user enters the amount owed and the amount the purchaser paid and the change due, followed by the amount of dollars, quarters, nickles, dimes, and pennies are displayed. I have been working on this for a while and understand the subtracting the paid from the owed....the rest...im lost... A picture of my layout is displayed after my code

Thanks for the help!

Here is my code:

Public Class Form1

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    txtOwed.Text = String.Empty
    txtPaid.Text = String.Empty
    txtChange.Text = String.Empty
    txtDollars.Text = String.Empty
    txtQuarters.Text = String.Empty
    txtDimes.Text = String.Empty
    txtNickles.Text = String.Empty
    txtPennies.Text = String.Empty

End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim decOwed As Decimal
    Dim decPaid As Decimal
    Dim decChange As Decimal
    Dim intDollars As Integer
    Dim intQuarters As Integer
    Dim intDimes As Integer
    Dim intNickels As Integer
    Dim intPennies As Integer

    Const dollars = 1
    Const quarters = 25
    Const dimes = 10
    Const nickels = 5
    Const pennies = 100

    decChange = decOwed - decPaid
    txtChange.Text = decChange

    intDollars = Val(txtDollars.Text)
    intQuarters = Val(txtQuarters.Text)
    intDimes = Val(txtDimes.Text)
    intNickels = Val(txtNickles.Text)
    intPennies = Val(txtPennies.Text)
    decChange = Val(txtChange.Text)

    intDollars = intPennies / 100
    txtDollars.Text = intDollars.ToString

    intQuarters = intPennies / 25
    txtQuarters.Text = intQuarters.ToString


    intDimes = intPennies / 10
    txtDimes.Text = intDimes.ToString

    intNickels = intPennies / 5
    txtNickles.Text = intNickels.ToString

    intDollars = intPennies / 1
    txtPennies.Text = intPennies.ToString

End Sub

Private Sub txtOwed_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtOwed.KeyPress
    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
        e.KeyChar <> ControlChars.Back AndAlso
        e.KeyChar <> "$" AndAlso
        e.KeyChar <> "." Then
        e.Handled = True
    End If
End Sub

End Class


4 Years
Discussion Span
Last Post by Reverend Jim

Look at it as an array problem. Your array contains the various denominations in cents. Your possible change values are dollars (100), quarters (25), etc. so your array is

Dim denom() As Integer = {100, 25, 10, 5, 1}

Then you calculate the difference between what is owed and what was paid (in cents). Now instead of hard coding the test for each denomination you can step through the array values and try subtracting multiples of each denomination in turn if possible, keeping track of how many times you subtract each one.

That's one way. Another would be to step through each denomination and use integer divide to determine the number of each.

If you are stuck with the algorithm then do it manually on paper then reproduce the steps in code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.