Hi everone!, im currently working on a cash register program wich has to meet certain tests. The problem im encountering is when im calculating the change to be given to the customer. The change is correct but it has to be displayed in such a way as to give the least amount of denomination, in other words logical! It does display the most logical denomination for nearly all transactions but some like

EG cost=£32
tendered=£50
Change=£18

problem

instead of displaying change as £10 & £5 & £2 & £1, it does the last £1 in small change!

here is the section of code, i know its pretty dodgy but i aint got time to change the whole thing - IF ANY KIND PEOPLE COULD OFFER ME A QUICK FIX SOLUTION I WOULD APPRECIATE YOUR HELP A LOT, THANXS IN ADVANCE!!!

``````Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Do
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Dim cost As Double = CType(Me.txtCost.Text, Double)
Dim amountTended As Double
Dim Change As Double
Dim i As Double

' Get the value of the total order
cost = CDec(Me.txtCost.Text)

' The amount tended will be entered by the user
amountTended = CType(Me.txtAmounttended.Text, Integer)

' Calculate the difference of both values
Change = amountTended - cost

' Display the result in the change text box
Me.txtChange.Text = Change.ToString("C")

'Validate Data

If cost > 150 Or cost = 0 Then

Else
Change = 150 - cost
End If

If cost > amountTended Then

Else
Change = amountTended - cost
End If

'count the number of notes and coins in the change (process repeated for each denomination)

While Change > 50
Change = Change - 50
i = i + 1
End While
txt50.Text = i & " "
i = 0

While Change > 20
Change = Change - 20
i = i + 1
End While
txt20.Text = i & " "
i = 0

While Change > 10
Change = Change - 10
i = i + 1
End While
txt10.Text = i & " "
i = 0

While Change > 5
Change = Change - 5
i = i + 1
End While
txt5.Text = i & " "
i = 0

While Change > 2
Change = Change - 2
i = i + 1
End While
txt2.Text = i & " "
i = 0

While Change > 1
Change = Change - 1
i = i + 1
End While
txt1.Text = i & " "
i = 0

While Change > 0.5
Change = Change - 0.5
i = i + 1
End While
txt50p.Text = i & " "
i = 0

While Change > 0.2
Change = Change - 0.2
i = i + 1
End While
txt20p.Text = i & " "
i = 0

While Change > 0.1
Change = Change - 0.1
i = i + 1
End While
txt10p.Text = i & " "
i = 0

While Change > 0.05
Change = Change - 0.05
i = i + 1
End While
txt5p.Text = i & " "
i = 0

While Change > 0.02
Change = Change - 0.02
i = i + 1
End While
txt2p.Text = i & " "
i = 0

Dim Pence As Integer = CInt(Change * 100)
txt1p.Text = Pence & " "``````

Hi csgal,

First of all the code you provided would never work well as the change variable is being calculated badly. The unneccassary modification of the Change variable happens twice at these locations:

['Validate Data

If cost > 150 Or cost = 0 Then

Else
Change = 150 - cost
End If

If cost > amountTended Then

Else
Change = amountTended - cost
End If]

Both the lines :
Change = 150 - cost and
Change = amountTended - cost have to be completely removed as they modify the change variable unneccassarily giving the wrong change answer. After solving that, the problem you mentioned came up. the solution is simple:

Where you have code like:

[While Change > 50
Change = Change - 50
i = i + 1
End While
txt50.Text = i & " "
i = 0]

add an equals sign to >50 i.e. it would become this code:

[While Change >= 50
Change = Change - 50
i = i + 1
End While
txt50.Text = i & " "
i = 0]

After doing that modification it worked perfectly for me.

Btw, i don't know if its an error or if its intentional but changing the amounttended variable into integer is causing the result to ignore any fractions in the number. I guess its intended though.

Hope this has solved your problem, and send any queries you may still have.

Thanks,