6 Replies - 10252 Views - Last Post: 24 September 2012 - 09:22 AM Rate Topic: -----

#1 jake_98gt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-November 06

VB.NET money changer

Posted 29 November 2006 - 11:43 PM

I'm having some issues with coding a money changer in vb.net. I'm trying to make a windows application to make change for a dollar. My problem is that when the program is run it doesn't return the correct cents. example>>>>

Amount: 6

half dollars: 1
quarters: 1
dimes: 1
nickels: 1
pennies: 1

these are the results. But this only adds up to 91 cents. I'm missing 3 pennies.

here's the code
 Private Sub change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles change.Click
		Dim shrhalfdollars, shrquarters, shrdimes, shrnickels, shrpennies, shrchange As Short
		shrchange = 100 - sale.Value
		If shrchange >= 50 Then
			shrhalfdollars = 1
			shrchange = shrchange - 50
		End If
		If shrchange >= 25 Then
			shrquarters = 1
			shrchange = shrchange - 25
		End If
		If shrchange >= 10 Then
			shrdimes = 1
			shrchange = shrchange - 10
		End If
		If shrchange >= 5 Then
			shrnickels = 1
			shrchange = shrchange - 5
		End If
		If shrchange >= 1 Then
			shrpennies = 1
			shrchange = shrchange - 1
		End If
		halfdollars.Text = shrhalfdollars
		quarters.Text = shrquarters
		dimes.Text = shrdimes
		nickels.Text = shrnickels
		pennies.Text = shrpennies

	End Sub


I know the dimes and pennies shouldn't equal 1 but if i use a do while for the dimes and pennies the program freezes.
here is what i had for the do while. (everything is the same in the code except for the dimes and pennies)

Do While shrchange >= 10
			shrdimes = 0
			shrchange = shrdimes + 1
		Loop

Do While shrchange >= 1
			shrpennies = 0
			shrchange = shrpennies + 1
		Loop 



any assistance would be greatly appreciated!

This post has been edited by jake_98gt: 30 November 2006 - 12:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET money changer

#2 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: VB.NET money changer

Posted 30 November 2006 - 07:26 AM

You were super close. a few mistakes I noticed that you never incrimented the count of coins (shrpennies = 1 instead of shrpennies = shrpennies + 1).

Your while statement was the right path, except you went way off on it. First you need While...End While instead of Do While...Loop . You also had a few wrong variables in the while loops (shrchange = shrdimes + 1). You were adding when you should have been subtracting, and all kinds of stuff *LoL. If you are like me though, your code probably just ended up that way trying to debug it

Anyway, here is a slightly revised version that should work

Tip For Shorter Code:
Use += to incriment and -= to decriment

Private Sub change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles change.Click
		Dim shrhalfdollars, shrquarters, shrdimes, shrnickels, shrpennies, shrchange As Short
		shrchange = 100 - sale.Value
		If shrchange >= 50 Then
			shrhalfdollars += 1
			shrchange -= 50
		End If
		If shrchange >= 25 Then
			shrquarters += 1
			shrchange = shrchange - 25
		End If
		While shrchange >= 10
			shrdimes += 1
			shrchange -= 10
		End While
		If shrchange >= 5 Then
			shrnickels += 1
			shrchange -= 5
		End If
		While shrchange >= 1
			shrpennies += 1
			shrchange -= 1
		End While
		halfdollars.Text = shrhalfdollars
		quarters.Text = shrquarters
		dimes.Text = shrdimes
		nickels.Text = shrnickels
		pennies.Text = shrpennies

	End Sub

This post has been edited by the_hangman: 30 November 2006 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 jake_98gt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-November 06

Re: VB.NET money changer

Posted 03 December 2006 - 07:35 PM

I appreciate the help with that. And actually the answer I had originally was what the solution booklet had so I went and worked it out with the instructor.

View Postthe_hangman, on 30 Nov, 2006 - 07:26 AM, said:

You were super close. a few mistakes I noticed that you never incrimented the count of coins (shrpennies = 1 instead of shrpennies = shrpennies + 1).

Your while statement was the right path, except you went way off on it. First you need While...End While instead of Do While...Loop . You also had a few wrong variables in the while loops (shrchange = shrdimes + 1). You were adding when you should have been subtracting, and all kinds of stuff *LoL. If you are like me though, your code probably just ended up that way trying to debug it

Anyway, here is a slightly revised version that should work

Tip For Shorter Code:
Use += to incriment and -= to decriment

Private Sub change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles change.Click
		Dim shrhalfdollars, shrquarters, shrdimes, shrnickels, shrpennies, shrchange As Short
		shrchange = 100 - sale.Value
		If shrchange >= 50 Then
			shrhalfdollars += 1
			shrchange -= 50
		End If
		If shrchange >= 25 Then
			shrquarters += 1
			shrchange = shrchange - 25
		End If
		While shrchange >= 10
			shrdimes += 1
			shrchange -= 10
		End While
		If shrchange >= 5 Then
			shrnickels += 1
			shrchange -= 5
		End If
		While shrchange >= 1
			shrpennies += 1
			shrchange -= 1
		End While
		halfdollars.Text = shrhalfdollars
		quarters.Text = shrquarters
		dimes.Text = shrdimes
		nickels.Text = shrnickels
		pennies.Text = shrpennies

	End Sub

Was This Post Helpful? 0
  • +
  • -

#4 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: VB.NET money changer

Posted 07 December 2006 - 09:22 AM

If it works, the solution itself usaly do'nt matter. I guess if its easyer to you, you can keep it as it is, but this could be done also something like this:

Private Sub change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles change.Click
:::dim muutuja=96
:::halfdollars.Text = väärtusta(muutuja, 50)
:::quarters.Text = väärtusta(muutuja, 25)
:::dimes.Text = väärtusta(muutuja, 10)
:::nickels.Text = väärtusta(muutuja, 5)
:::pennies.Text = väärtusta(muutuja, 1)
end sub

function väärtusta(byref x,byval y)
:::dim z=(x - (x mod y))/y
:::x=x-z*y
:::väärtusta=z
end function

This post has been edited by m2s87: 07 December 2006 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 papermater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-March 07

Re: VB.NET money changer

Posted 28 March 2007 - 09:45 AM

I too am doing a similar project, though my problem is that we have to develop a subroutine using pass-by reference.

Here's my code:

Public Class makeChangeForm

	Private Sub computeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
		' Declaring variables for currency amounts
		Dim PurchaseAmount As Decimal
		Dim AmountPaid As Decimal

		Dim dblFiftyDollars As Double
		Dim dblTwentyDollars As Double
		Dim dblTenDollars As Double
		Dim dblFiveDollars As Double
		Dim dblDollar As Double
		Dim dblQuarter As Double
		Dim dblDime As Double
		Dim dblPenny As Double

		Dim strFiftyDollars As String
		Dim strTwentyDollars As String
		Dim strTenDollars As String
		Dim strFiveDollars As String
		Dim strDollar As String
		Dim strQuarter As String
		Dim strDime As String
		Dim strPenny As String
		Dim changeDue As String

		' Initial value of currencies
		dblFiftyDollars = 0
		dblTwentyDollars = 0
		dblTenDollars = 0
		dblFiveDollars = 0
		dblDollar = 0
		dblQuarter = 0
		dblDime = 0
		dblPenny = 0

		' retrieve user input
		PurchaseAmount = Val(purchasePriceTextBox.Text)
		AmountPaid = Val(amountPaidTextBox.Text)

		' This calculates how much change should be returned to the customer.
		ChangeDue = AmountPaid - PurchaseAmount

		'The following Do Until Loops calculate the amount of the different types
		' of money that needs to be given back to the customer.

		Do Until changeDue < 50.0
			dblFiftyDollars += 1
			changeDue -= 50.0
		Loop
		Do Until changeDue < 20.0
			dblTwentyDollars += 1
			changeDue -= 20.0
		Loop
		Do Until changeDue < 10.0
			dblTenDollars += 1
			changeDue -= 10.0
		Loop
		Do Until changeDue < 5.0
			dblFiveDollars += 1
			changeDue -= 5.0
		Loop
		Do Until changeDue < 1.0
			dblDollar += 1
			changeDue -= 1.0
		Loop
		Do Until changeDue < 0.25
			dblQuarter += 1
			changeDue -= 0.25
		Loop
		Do Until changeDue < 0.1
			dblDime += 1
			changeDue -= 0.1
		Loop
		Do While ChangeDue > 0.0
			dblPenny += 1
			changeDue -= 0.01
		Loop
		'Converts change totals to string increments
		strFiftyDollars = Convert.ToString(dblFiftyDollars)
		strTwentyDollars = Convert.ToString(dblTwentyDollars)
		strTenDollars = Convert.ToString(dblTenDollars)
		strFiveDollars = Convert.ToString(dblTenDollars)
		strDollar = Convert.ToString(dblDollar)
		strQuarter = Convert.ToString(dblQuarter)
		strDime = Convert.ToString(dblDime)
		strPenny = Convert.ToString(dblPenny)

		' set output headers
		outputTextBox.Text = "$50 Bills: " & strFiftyDollars & vbCrLf
		outputTextBox.Text &= "$20 Bills: " & strTwentyDollars & vbCrLf
		outputTextBox.Text &= "$10 Bills: " & strTenDollars & vbCrLf
		outputTextBox.Text &= "$5 Bills: " & strFiveDollars & vbCrLf
		outputTextBox.Text &= "$1 Bills: " & strDollar & vbCrLf
		outputTextBox.Text &= " Quarters: " & strQuarter & vbCrLf
		outputTextBox.Text &= " Dimes: " & strDime & vbCrLf
		outputTextBox.Text &= "Pennies: " & strPenny & vbCrLf
	End Sub
End Class




What do I need to do so that I can have the program still work, and it have pass-by references?

This post has been edited by papermater: 28 March 2007 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 fireten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-September 12

Re: VB.NET money changer

Posted 24 September 2012 - 07:56 AM

There is a much easier way of doing it and it shorter thank me if i helped it will do the whole job

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Double = Textbox.Text
        Dim y, z, f, r As Double
        Quarter.Text = x \ 25
        y = x Mod 25
        Dime.Text = y \ 10
        z = y Mod 10
        Nickel.Text = z \ 5
        f = z Mod 5
        Pennie.Text = f \ 1
        r = f Mod 1



    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: VB.NET money changer

Posted 24 September 2012 - 09:22 AM

VB.net isn't vb6 is it now?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1