Hi guys/gals, i'm new here. I have a class project that needs the following to be performed:

A form will accept two types of input then perform a calcuation. The two inputs are,

1- How much money a customer owes me

2. How much money the customer paid/tendered

The program must then calculate the change that is due, but it must be displayed in 5 fields..

example -

$5.50 due, customer pays $10

Program must show -

Dollars owed - 4

Quarters owed - 2

Dimes owed - 0

Nickeles owed - 0

Pennies owed - 0

Of course, the input amounts will vary and the program needs to be able to calculate various inputs.

I already have the interface done, but the problem i am having is the coding going into the calculation button..

My problem is trying to figure out how to work with the change portion of the total amount to be paid back..

My program can figure out how to display dollars owed to customer, but I can't figure out how to display the correct change.

Here is my code so far for the calculation button -

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

'Declaring my variables

Dim decTotalOwed As Decimal

Dim decTotalPaid As Decimal

Dim decPayBack As Decimal

Dim decDollarsOwed As Decimal

Dim decQuartersOwed As Decimal

Dim decDimesOwed As Decimal

Dim decNickelsOwed As Decimal

Dim decPenniesOwed As Decimal

Dim decTemp As Decimal

Dim decPayBack2, intPayBack3, intPayback4 As Decimal

'Assigning some of the variables

decTotalOwed = DueBox.Text

decTotalPaid = PaidBox.Text

decPayBack = decTotalPaid - decTotalOwed

'Assuring the customer didn't short change us with a message box"

If decTotalOwed > decTotalPaid Then

MsgBox("The customer owes you more money, or you did not input amounts correctly!")

End If

'Assigning more values to the variables

decDollarsOwed = decPayBack \ 1

DollarField.Text = decDollarsOwed

decQuartersOwed = (decPayBack - decDollarsOwed) \ 0.25

QuarterField.Text = decQuartersOwed

'Added this code to see what variables are holding what values, 'for testing

totalbox.Text = decPayBack

'Work in progress from below this point

'decDimesOwed = intPayBack Mod 10

'DimeField.Text = intDimesOwed

Please help, even if you have little knowledge.. I can't figure out how to store the change due as a variable and then subdivide it in quarters, nickels, etc..

## 10 Replies - 26700 Views - Last Post: 20 September 2007 - 12:57 AM

##
**Replies To:** Change Calculator

### #2

## Re: Change Calculator

Posted 16 February 2007 - 01:09 PM

Dim myChange as Double ' all your programming code ' Private Sub MakeChange () ' assumes you have already Calculated Dollars DIM myQuarters, myDimes, myNickles, myPennies While MyChange > 0 'As each coin is Calculated the value is removed until you reach 0 Select Case MyChange ' Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES Case > 25 myQuarters += 1 MyChange -= .25 Case > 10 myDimes += 1 myChange -= .10 Case > 5 ' do nickles Case > 1 ' do pennies End Select Wend

This post has been edited by **KeyWiz**: 16 February 2007 - 01:12 PM

### #3

## Re: Change Calculator

Posted 16 February 2007 - 03:52 PM

KeyWiz, on 16 Feb, 2007 - 01:09 PM, said:

Dim myChange as Double ' all your programming code ' Private Sub MakeChange () ' assumes you have already Calculated Dollars DIM myQuarters, myDimes, myNickles, myPennies While MyChange > 0 'As each coin is Calculated the value is removed until you reach 0 Select Case MyChange ' Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES Case > 25 myQuarters += 1 MyChange -= .25 Case > 10 myDimes += 1 myChange -= .10 Case > 5 ' do nickles Case > 1 ' do pennies End Select Wend

Thank you very much!

### #4

## Re: Change Calculator

Posted 17 February 2007 - 07:44 PM

Hi again, I just tried implementing that sample code into my calculate button and nothing happened. None of the textbox fields displayed anything, not even zeroes.. Here is what my code looks like:

Private Sub Calculate_Button(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myChange As Double ' all your programming code Dim myDollars, myQuarters, myDimes, myNickles, myPennies As Integer While myChange > 0 'As each coin is Calculated the value is removed until you reach 0 Select Case myChange 'Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES Case Is > 1 myDollars += 1 myChange -= 1 DollarField.Text = myDollars Case Is > 0.25 myQuarters += 1 myChange -= 0.25 QuarterField.Text = myQuarters Case Is > 0.1 myDimes += 1 myChange -= 0.1 DimeField.Text = myDimes Case Is > 0.05 myNickles += 0.05 myChange -= 0.05 NickelField.Text = myNickles Case Is > 0.01 myPennies += 0.01 myChange -= 0.01 PennyField.Text = myPennies End Select End While End Sub

### #5

## Re: Change Calculator

Posted 18 February 2007 - 05:29 PM

Sorry, my bad

I was using .NET code

do it like this -

Again, sorry

I was using .NET code

do it like this -

Select Case myChange 'Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES Case Is >= 1 myDollars = myDollars + 1 myChange = myChange - 1 DollarField.Text = myDollars

Again, sorry

This post has been edited by **KeyWiz**: 18 February 2007 - 05:31 PM

### #6

## Re: Change Calculator

Posted 18 February 2007 - 06:52 PM

No need to apologize, after all, you are helping me!

My question is this.. I'm not sure how to declare or assign mychangevalue. From the sample code you provided, it's being declared as a double, what does a double variable do as opposed to integer?

Here is my current code, maybe you can help me figure out why it's not working?

Dim myChange As Double

' all your programming code

Dim myDollars, myQuarters, myDimes, myNickles, myPennies As Integer

While myChange > 0

'As each coin is Calculated the value is removed until you reach 0

Select Case myChange

'Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES

Case Is >= 1

myDollars = myDollars + 1

myChange = myChange - 1

DollarField.Text = myDollars

Shouldn't myChange = val(total2payback) ??? I'm not sure though, but i look foward to your helping reply!

My question is this.. I'm not sure how to declare or assign mychangevalue. From the sample code you provided, it's being declared as a double, what does a double variable do as opposed to integer?

Here is my current code, maybe you can help me figure out why it's not working?

Dim myChange As Double

' all your programming code

Dim myDollars, myQuarters, myDimes, myNickles, myPennies As Integer

While myChange > 0

'As each coin is Calculated the value is removed until you reach 0

Select Case myChange

'Case = ANY COIN SIZE YOU LIKE OR EVEN DOLLAR VALUES

Case Is >= 1

myDollars = myDollars + 1

myChange = myChange - 1

DollarField.Text = myDollars

Shouldn't myChange = val(total2payback) ??? I'm not sure though, but i look foward to your helping reply!

### #7

## Re: Change Calculator

Posted 28 February 2007 - 03:24 PM

I'm working on something very similar. However we cannot use Case statements. that being we have not got to them yet.

So I have to use mathmatical funtions to computer each variable step by step. My problem is that I am getting a "devide by 0 exception".

Example is that I have a total of $50.33 with $55.00 tendered. Change Due is $4.67.

To compute "dollars" owed I used "dollarsChange = changeDue \ 1"

That being a division function to return an integer minus it's remainder. This should have returned 4$ (at least that is how the book explains it the function of integer division. However the answer returned was 5$

I am confused as my book states such a division should have dropped the remainder 67 cents not round up to the next whole. Anyone care to point me in the right direction?

So I have to use mathmatical funtions to computer each variable step by step. My problem is that I am getting a "devide by 0 exception".

Example is that I have a total of $50.33 with $55.00 tendered. Change Due is $4.67.

To compute "dollars" owed I used "dollarsChange = changeDue \ 1"

That being a division function to return an integer minus it's remainder. This should have returned 4$ (at least that is how the book explains it the function of integer division. However the answer returned was 5$

I am confused as my book states such a division should have dropped the remainder 67 cents not round up to the next whole. Anyone care to point me in the right direction?

### #8

## Re: Change Calculator

Posted 28 February 2007 - 03:33 PM

OK for now I'm doing thus but it still doesn't feel like the "right answer.

changeReminaing = changeDue Mod 1

dollarsChange = changeDue - changeRemaining

Me.xDollarsDueLabel.Text = dollarsChagne.ToString

Of course this still leaves me to do all the others but at least now I get a properly computed integer without any rounding to cause errors.

changeReminaing = changeDue Mod 1

dollarsChange = changeDue - changeRemaining

Me.xDollarsDueLabel.Text = dollarsChagne.ToString

Of course this still leaves me to do all the others but at least now I get a properly computed integer without any rounding to cause errors.

### #9

## Re: Change Calculator

Posted 28 February 2007 - 04:26 PM

Well I used the mod function throughout and all functions well but it still seams to me that there ought to have been a cleaner method.

I'm still open to suggestions should anyone have any.

I'm still open to suggestions should anyone have any.

### #10

## Re: Change Calculator

Posted 28 February 2007 - 07:09 PM

Nightshade, on 28 Feb, 2007 - 04:26 PM, said:

Well I used the mod function throughout and all functions well but it still seams to me that there ought to have been a cleaner method.

I'm still open to suggestions should anyone have any.

I'm still open to suggestions should anyone have any.

Well after much frustration and with help from another student, I got my project working. Here is the source code for the Calculation Button...

'Declaring my variables Dim decAmountDue, decAmountReceived, decChangeDue As Decimal Dim intDollars, intQuarters, intDimes, intNickels, intPennies As Integer 'A variable to temporarily store the change for each calculation Dim intTempValue As Integer 'Calculates the total amount of change due to customer decAmountdue = Val(DueBox.Text) decAmountReceived = Val(PaidBox.Text) decChangeDue = decAmountReceived - decAmountDue 'Displays a messagebox letting us know the customer didn't give us enough money If decAmountReceived < decAmountdue Then MessageBox.Show("Customer shortchanged us, or you entered something wrong!", "Hey!") 'Clears the text fields to start over Call Clear() End If 'Formats the proper fields to display numbers as currency format DueBox.Text = Format(decAmountDue, "Currency") PaidBox.Text = Format(decAmountReceived, "currency") totalbox.Text = Format(decChangeDue, "currency") 'Converts change due value into pennies intTempvalue = decChangeDue * 100 'Calculates our dollars owed field intDollars = intTempvalue \ 100 DollarField.Text = intDollars 'Calculates quarters owed minus dollars we are going to pay intTempValue = intTempValue Mod 100 'Calc Quarters remaining minus dollars paid intQuarters = intTempvalue \ 25 QuarterField.Text = intQuarters 'Calculate the number of dimes intTempValue = intTempValue Mod 25 'Calc dimes remaining minus quarters paid intDimes = intTempvalue \ 10 DimeField.Text = intDimes 'Calculate nickels intTempValue = intTempValue Mod 10 'Calc nickels remaining minus dimes paid intNickels = intTempvalue \ 5 NickelField.Text = intNickels 'Calculate pennies intTempValue = intTempValue Mod 5 'Calc pennies remaining minus nickels paid intPennies = intTempvalue PennyField.Text = intPennies

### #11

## Re: Change Calculator

Posted 20 September 2007 - 12:57 AM

First of all: My code isn't exactly VB but very much alike, let's call it a VB derivate.

I created almost the same code, it looks like this:

In turn dValue will be filled with the value of the next banknote or coin (in my case (in €) 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01)

The function ModInv I created myself and it looks like this:

This all works beautifully but now comes the hard part:

I added checkboxes for each possible dValue in which a cashier can make clear he or she is lacking some coins or bills. Now I want to recalculate the whole traject ... but how?

For example:

I need to return 8 cents but I got no 1 cent coins. So, the code has to stop using the 5 cent coin (that can be arranged) and instead use 4 times a 2 cent coin.

Worse example:

I need to return 11 cents. My code uses a 10 cent coin, skips the 5 and the 2 cent and uses a 1 cent coin. Now I still got no 1 cent coins. So, in this case I have to tell my code NOT to use the 10 cent coin anymore (that can be arranged). Now my code decides to use 2 times a 5 cent coin and still the calculation gets messed up.

This problem intensifies on 'the way up'.

21 cent to return can't use the 20, the 10 (not 2 times that is) nor the 5 cent coin (not 4 times that is).

51 cent to return can't use the 50, the 10 (not 5 times that is) nor the 5 cent coin (not 10 times that is), etc.

I created almost the same code, it looks like this:

For i = 1 To 12 iTimes = ModInv(dChange, dValue) If iTimes Then dChange = dChange - (iTimes * dValue) Next i

In turn dValue will be filled with the value of the next banknote or coin (in my case (in €) 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01)

The function ModInv I created myself and it looks like this:

Function ModInv(dGetal As Double, dDivideThrough As Double) As Double ModInv = (dGetal - Mod(dGetal, dDivideThrough)) / dDivideThrough End Function

This all works beautifully but now comes the hard part:

I added checkboxes for each possible dValue in which a cashier can make clear he or she is lacking some coins or bills. Now I want to recalculate the whole traject ... but how?

For example:

I need to return 8 cents but I got no 1 cent coins. So, the code has to stop using the 5 cent coin (that can be arranged) and instead use 4 times a 2 cent coin.

Worse example:

I need to return 11 cents. My code uses a 10 cent coin, skips the 5 and the 2 cent and uses a 1 cent coin. Now I still got no 1 cent coins. So, in this case I have to tell my code NOT to use the 10 cent coin anymore (that can be arranged). Now my code decides to use 2 times a 5 cent coin and still the calculation gets messed up.

This problem intensifies on 'the way up'.

21 cent to return can't use the 20, the 10 (not 2 times that is) nor the 5 cent coin (not 4 times that is).

51 cent to return can't use the 50, the 10 (not 5 times that is) nor the 5 cent coin (not 10 times that is), etc.

This post has been edited by **XJ12L**: 20 September 2007 - 02:33 AM

Page 1 of 1