Hello.
As the title states, i am unsure of which sort of loop to use in this case.
my little program asks for the users input (into textbox's) the cost of a purchase and what they paid.
the program is then intended to generate what change they would recieve be it either 1x $50 note, 1x $5 note, 1x $0.50 Coin. etc.
but i cant seem to get the loop right, which would generate this string..
Any help? :)
code is:

Public Class Change
    Private Sub txtCost_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCost.KeyPress
        Dim allowedchars As String = "0123456789."
        If allowedchars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtMoney_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMoney.KeyPress
        Dim allowedchars As String = "0123456789."
        If allowedchars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub
    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If rdbNo.Checked = True Then
            MsgBox(DontGetExactChange(), MsgBoxStyle.OkOnly, "Change..")
        ElseIf rdbYes.Checked = True Then
            MsgBox(GetExactChange(), MsgBoxStyle.OkOnly, "Change..")
        Else
            MsgBox(DontGetExactChange(), MsgBoxStyle.OkOnly, "Change..")
        End If
        End Sub
    Private Function GetChange(ByVal x)
        Dim changeReturned As String = ""
        Dim change As Integer = x
        Do Until change = 0
            If x >= 50 Then
                changeReturned = changeReturned + "$50 Note" + vbCrLf
                change = change - 50
            ElseIf x >= 20 Then
                changeReturned = changeReturned + "$20 Note" + vbCrLf
            ElseIf x >= 10 Then
                changeReturned = changeReturned + "$10 Note" + vbCrLf
            ElseIf x >= 5 Then
                changeReturned = changeReturned + "$5 Note" + vbCrLf
            ElseIf x >= 2 Then
                changeReturned = changeReturned + "$2 Coin" + vbCrLf
            ElseIf x >= 1 Then
                changeReturned = changeReturned + "$1 Coin" + vbCrLf
            ElseIf x >= 0.5 Then
                changeReturned = changeReturned + "50c Coin" + vbCrLf
            ElseIf x >= 0.2 Then
                changeReturned = changeReturned + "20c Coin" + vbCrLf
            ElseIf x >= 0.1 Then
                changeReturned = changeReturned + "10c Coin" + vbCrLf
            ElseIf x >= 0.05 Then
                changeReturned = changeReturned + "5c Coin" + vbCrLf
            End If
        Loop
        Return changeReturned
    End Function
    Private Function GetExactChange()
        Dim cost As Decimal
        Dim money As Decimal
        Dim change As Decimal
        Dim text As String = ""
        cost = txtCost.Text
        money = txtMoney.Text
        change = money - cost
        text = "Purchasing something with a value of $" & cost & " and paying $" & money & " will result in a total of $" & change & " change." & vbCrLf & GetChange(change)
        Return text
    End Function
    Private Function DontGetExactChange()
        Dim cost As Decimal
        Dim money As Decimal
        Dim change As Decimal
        Dim text As String = ""
        cost = txtCost.Text
        money = txtMoney.Text
        change = money - cost
        text = "Purchasing something with a value of $" & cost & " and paying $" & money & " will result in a total of $" & change & " change."
        Return text
    End Function
End Class

Edited 4 Years Ago by Gobble45: n/a

I would create an array of denominations ordered from largest to smallest. Then I would create a while loop that runs while the balance (cash - cost) is greater than zero. An inner loop would select denominations from largest to smallest and determine how many multiples of each could be subtracted from the current balance. It would subtract (and keep track of) each multiple. That would eliminate all of the if-then-else constructs.

So the short answer is a For loop inside of a While loop.

Edited 4 Years Ago by Reverend Jim: n/a

I should know better than to do this while distracted. You just need a for loop. Just work down from greatest to least denomination and figure out how many of each (if any) can be subtracted from the current balance, then subtract that amount and continue. Because your last denomination is one penny, when you are done the for loop the balance will be zero. If you use a dictionary where the key is the denomination and the value is the word as in

denom(10000) = "Hundreds"
.
.
.
denom(1) = "Pennies"

Then you have both the denomination for the calculation and the output word at each iteration of the loop.

My apologies for not giving this my full attention:$

Edited 4 Years Ago by Reverend Jim: n/a

This article has been dead for over six months. Start a new discussion instead.