VB Change Calulator
- by BlueBeast
I am creating a VB 2008 change calculator as an assignment. The program is to use the amount paid - the amount due to calculate the total.(this is working fine). After that, it is to break that amount down into dollars, quarters, dimes, nickels, and pennies. The problem I am having is that sometimes the quantity of pennies, nickels or dimes will be a negative number. For example $2.99 = 3 Dollars and -1 Pennies.
SOLVED
Thanks to the responses, here is what I was able to make work with my limited knowledge.
Option Explicit On
Option Strict Off
Option Infer Off
Public Class frmMain
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clear boxes
lblDollarsAmount.Text = String.Empty
lblQuartersAmount.Text = String.Empty
lblDimesAmount.Text = String.Empty
lblNickelsAmount.Text = String.Empty
lblPenniesAmount.Text = String.Empty
txtOwed.Text = String.Empty
txtPaid.Text = String.Empty
lblAmountDue.Text = String.Empty
txtOwed.Focus()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Close application'
Me.Close()
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' Find Difference between Total Price and Total Received
lblAmountDue.Text = Val(txtPaid.Text) - Val(txtOwed.Text)
Dim intChangeAmount As Integer = lblAmountDue.Text * 100
'Declare Integers
Dim intDollarsBack As Integer
Dim intQuartersBack As Integer
Dim intDimesBack As Integer
Dim intNickelsBack As Integer
Dim intPenniesBack As Integer
' Change Values
Const intDollarValue As Integer = 100
Const intQuarterValue As Integer = 25
Const intDimeValue As Integer = 10
Const intNickelValue As Integer = 5
Const intPennyValue As Integer = 1
'Dollars
intDollarsBack = CInt(Val(intChangeAmount \ intDollarValue))
intChangeAmount = intChangeAmount - Val(Val(intDollarsBack) * intDollarValue)
lblDollarsAmount.Text = intDollarsBack.ToString
'Quarters
intQuartersBack = CInt(Val(intChangeAmount \ intQuarterValue))
intChangeAmount = intChangeAmount - Val(Val(intQuartersBack) * intQuarterValue)
lblQuartersAmount.Text = intQuartersBack.ToString
'Dimes
intDimesBack = CInt(Val(intChangeAmount \ intDimeValue))
intChangeAmount = intChangeAmount - Val(Val(intDimesBack) * intDimeValue)
lblDimesAmount.Text = intDimesBack.ToString
'Nickels
intNickelsBack = CInt(Val(intChangeAmount \ intNickelValue))
intChangeAmount = intChangeAmount - Val(Val(intNickelsBack) * intNickelValue)
lblNickelsAmount.Text = intNickelsBack.ToString
'Pennies
intPenniesBack = CInt(Val(intChangeAmount \ intPennyValue))
intChangeAmount = intChangeAmount - Val(Val(intPenniesBack) * intPennyValue)
lblPenniesAmount.Text = intPenniesBack.ToString
End Sub
End Class