hello every1! Im a newbie to vb.Net and i have done my best to workout a solution for this challenge but i keep hitting walls. i would appreciate it if some1 could help me with it, thanks in advance!!

My application is 99% complete except for this 1 problem:

the program is meant to calcualte exact change from the purchase and amount tended and then display the denomination of change in individual txtboxes ie 1 for each denomination

it calculates the change and displays it in a txtbox with no probs

Im having problems displaying just 1 pence's

for example: enter £1.99 as cost, enter £5 as tendered and it displays the correct change £3.01 but in the denomination txtboxes it displays a £2 coin and a £1 coin but no 1 penny, its the same for every amount i enter.

please help if u can and have a pleasant Easter!

heres what ive done so far...

Imports System.Math

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
Dim password As Short
Do
password = InputBox("Please Enter Your Log In Number")
Loop Until password = "1234" Or password = "5678"
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 Integer
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")

'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

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

End Sub

your code was ugly lol sorry so i wrote a new version for you. i hope you understand it. there were far to many mistakes in your code to explain what to fix, such as using a integer as the change value will round off the .xx (cents) and repeating the same loop a dozen times just with a different value is not good, and usually means there is a better way to write it. my way i just wrote up quick for you is just one way of doing it.

'Returns string of exact change
    Function CalculateChange(ByVal payment As Double, ByVal cost As Double) As String

        'array of every value of tender that can be returned
        Dim tenderValues As Double() = New Double() {100, 50, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01}

        'get change which is going to be returned
        Dim change As Double
        change = payment - cost

        'check that they payed enough
        If change < 0 Then
            Return "cost was greater then payment"
        End If

        'this is our output that will display what we are going to return
        Dim output As String = "Change to return" + vbNewLine

        'go through each tender value and calc return
        For Each t As Double In tenderValues

            'the whole number of tender value / change will give you how many of that value you can return
            Dim num As Integer = CInt(Math.Truncate(change / t))

            'if it can return atleast 1 report it then remove it from the change
            If (num > 0) Then
                output += t.ToString() + " : " + num.ToString() + vbNewLine
                change -= num * t
            End If

        Next

        'return our output and display it where needed such as a textbox
        Return output
    End Function

thanx for your input i am grateful for your time. Its my first year at college and i have no previous knowledge of .Net. But i definately agree that my code was definately left wanting (to say the least) lol. Any im not going to do a cut and paste jobbie i will try to learn from the u have given me. cya

I Understand your code and I would try to fix your problem but it seems someone has already done that. One Question, why did you use i = i + 1 or cost = cost - 50 instead of cost -= 50 or i -= 1?
If you need more help just uhh contact me somehow. First Time Posting

Nick Cammarata 14 yr. Old Programmer

Made This Just For You. Copy and Paste it into a Windows Application. I'm Only 14 so it might not be perfect

Public Class CashRegister
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtPrice As System.Windows.Forms.TextBox
Friend WithEvents txtTended As System.Windows.Forms.TextBox
Friend WithEvents btnCalculate As System.Windows.Forms.Button
Friend WithEvents lstchange As System.Windows.Forms.ListBox
Friend WithEvents lblchange As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtPrice = New System.Windows.Forms.TextBox()
Me.txtTended = New System.Windows.Forms.TextBox()
Me.btnCalculate = New System.Windows.Forms.Button()
Me.lstchange = New System.Windows.Forms.ListBox()
Me.lblchange = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'txtPrice
'
Me.txtPrice.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtPrice.Location = New System.Drawing.Point(8, 8)
Me.txtPrice.Name = "txtPrice"
Me.txtPrice.Size = New System.Drawing.Size(320, 29)
Me.txtPrice.TabIndex = 1
Me.txtPrice.Text = "Enter Price Of Item"
'
'txtTended
'
Me.txtTended.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtTended.Location = New System.Drawing.Point(8, 48)
Me.txtTended.Name = "txtTended"
Me.txtTended.Size = New System.Drawing.Size(320, 29)
Me.txtTended.TabIndex = 2
Me.txtTended.Text = "Enter Amount Tended"
'
'btnCalculate
'
Me.btnCalculate.BackColor = System.Drawing.Color.White
Me.btnCalculate.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnCalculate.ForeColor = System.Drawing.Color.Black
Me.btnCalculate.Location = New System.Drawing.Point(8, 128)
Me.btnCalculate.Name = "btnCalculate"
Me.btnCalculate.Size = New System.Drawing.Size(120, 32)
Me.btnCalculate.TabIndex = 3
Me.btnCalculate.Text = "Calculate"
'
'lstchange
'
Me.lstchange.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lstchange.ItemHeight = 25
Me.lstchange.Location = New System.Drawing.Point(344, 8)
Me.lstchange.Name = "lstchange"
Me.lstchange.Size = New System.Drawing.Size(208, 429)
Me.lstchange.TabIndex = 4
'
'lblchange
'
Me.lblchange.BackColor = System.Drawing.Color.White
Me.lblchange.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblchange.Location = New System.Drawing.Point(8, 88)
Me.lblchange.Name = "lblchange"
Me.lblchange.Size = New System.Drawing.Size(320, 32)
Me.lblchange.TabIndex = 5
Me.lblchange.Text = "Change : "
'
'CashRegister
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Black
Me.ClientSize = New System.Drawing.Size(992, 454)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblchange, Me.lstchange, Me.btnCalculate, Me.txtTended, Me.txtPrice})
Me.Name = "CashRegister"
Me.Text = "Cash Register"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
lstchange.Items.Clear()
CalculateChange()
End Sub
Private Sub CalculateChange()
Dim Quarter As Integer = 25
Dim Dime As Integer = 10
Dim Penny As Integer = 1
Dim Nickel As Integer = 5
Dim Dollar As Integer = 100
Dim FiveDollar As Integer = Dollar * 5
Dim TenDollar As Integer = Dollar * 10
Dim TwentyDollar As Integer = Dollar * 20
Dim FiftyDollar As Integer = Dollar * 50
Dim HundredDollar As Integer = Dollar * 100
Dim AmountTended, Price, Change As Double
Dim I As Double
'All The Amounts of money
Dim AmountOfPennies, AmountOfNickels, AmountOfDimes, AmountOfQuarters, AmountOfDollars, AmountOfFiveDollars _
, AmountOfTenDollars, AmountOfTwentyDollars, AmountOfFiftyDollars, AmountOfHundredDollars As Short
'Defines all of the variables in the textboxes
Try
Price = txtPrice.Text * 100
AmountTended = txtTended.Text * 100
If Price >= AmountTended Then
MsgBox("Price Is Above Amount Tended", "Error")
Else
Change = AmountTended - Price
lblchange.Text = "Change : " & Change
End If
Catch
MessageBox.Show("Make Sure Both TextBoxes Are Filled With Numbers", "Error")
End Try
Try
'Calculate Amount of hundred dollars
Do
If Change >= HundredDollar Then
Change -= HundredDollar
AmountOfHundredDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Hundreds: " & AmountOfHundredDollars)

'Calculate amount of fifty dollars
Do
If Change >= FiftyDollar Then
Change -= FiftyDollar
AmountOfFiftyDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Fifties: " & AmountOfFiftyDollars)

'Calculate amount of twenty dollars
Do
If Change >= TwentyDollar Then
Change -= TwentyDollar
AmountOfTwentyDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Twenties: " & AmountOfTwentyDollars)

'Calculate amount of ten dollars
Do
If Change >= TenDollar Then
Change -= TenDollar
AmountOfTenDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Tens: " & AmountOfTenDollars)
'Calculate amount of five dollars
Do
If Change >= FiveDollar Then
Change -= FiveDollar
AmountOfFiveDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Fives: " & AmountOfFiveDollars)
'Calculate amount of dollars
Do
If Change >= Dollar Then
Change -= Dollar
AmountOfDollars += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Dollars: " & AmountOfDollars)

'Calculate amount of Quarters
Do
If Change >= Quarter Then
Change -= Quarter
AmountOfQuarters += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Quarters : " & AmountOfQuarters)
'Calculate amount of Dimes
Do
If Change >= Dime Then
Change -= Dime
AmountOfDimes += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Dimes " & AmountOfDimes)
'Calculate amount of Nickels
Do
If Change >= Nickel Then
Change -= Nickel
AmountOfNickels += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Nickels : " & AmountOfNickels)
'Calculate amount of Pennies
Do
If Change >= Penny Then
Change -= Penny
AmountOfPennies += 1
Else
Exit Do
End If
Loop
lstchange.Items.Add("Pennies : " & AmountOfPennies)
Catch
End Try
End Sub
End Class

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