CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2009
    Posts
    1

    Exclamation Displaying the wrong results

    Hi
    I'm attempting the following code to calculate how many quarters, dimes, nickels and pennies a user should get as change, and display them in four labels, when buying an item from a machine. The user inserts the amount paid and the cost of the item in two textboxes.
    My code is as follows:
    Code:
     
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
            ' Variables for fields
            Dim lblCost As Double = CDbl(txtCostItm.Text)
            Dim lblAmnt As Double = CDbl(txtAmntGvn.Text)
    
    
            ' Find Difference between Cost of Item and Price Received
            Dim temp As Double = txtAmntGvn.Text - txtCostItm.Text
    
            'Declare Integers
    
            Dim lblQuar As Integer
            Dim lblDim As Integer
            Dim lblNick As Integer
            Dim lblPenn As Integer
    
          
    
    
            'How many quarters will be handed
            lblQuar = CInt(temp / 25)
            temp = temp - CDbl(CDbl(lblQuar) * 25)
            lblQrts.Text = lblQuar.ToString
    
            'Dimes 
            lblDim = CInt(temp / 10)
            temp = temp - CDbl(CDbl(lblDim) * 10)
            lblDimes.Text = lblDim.ToString
    
            'Nickels 
            lblNick = CInt(temp / 5)
            temp = temp - CDbl(CDbl(lblNick) * 5)
            lblNckls.Text = lblNick.ToString
    
            'Pennies
            lblPenn = CInt(temp / 1)
            temp = temp - CDbl(CDbl(lblPenn) * 1)
            lblPennies.Text = lblPenn.ToString
    I cant understand where I'm going wrong... anyone?!
    Thanks a lot, in advance.

  2. #2
    Join Date
    Aug 2005
    Location
    Spain!
    Posts
    149

    Re: Displaying the wrong results

    Try using mod & truncate division. Also the code will only work if the difference amount is specified in pennies

    Code:
            Dim lblCost As Double = CDbl(Txtcostitm.Text)
            Dim lblAmnt As Double = CDbl(txtAmntGvn.Text)
    
            ' Find Difference between Cost of Item and Price Received
            Dim temp As Double = txtAmntGvn.Text - Txtcostitm.Text
    
            'How many quarters will be handed
            lblQrts.Text = Math.Truncate(temp / 25)
            temp = temp Mod 25
    
            'Dimes 
            lblDimes.Text = Math.Truncate(temp / 10)
            temp = temp Mod 10
    
            'Nickels 
            lblNckls.Text = Math.Truncate(temp / 5)
            temp = temp Mod 5
    
            'Pennies
            lblPennies.Text = temp
    Maybe you've to change something, buy hope it helps as a base.

  3. #3
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: Displaying the wrong results

    If there is a decimal expected in the calcuation result, then the integer variable will ignore it. Put break point and step through the program to see where you are going wrong?
    If i helped you, Please rate me

    Visit http://www.octopusplus.com

  4. #4
    Join Date
    Apr 2008
    Posts
    82

    Re: Displaying the wrong results

    Code:
            Debug.WriteLine("Calculate Change")
            Dim decAMT As Decimal = 0.73D 'change
            Dim dec25, dec10, dec5, dec1 As Decimal 'quarters, dimes,nickles, pennies
            'calculate change for amount using Decimal
            dec25 = Math.Floor(decAMT / 0.25D)
            decAMT -= dec25 * 0.25D
            dec10 = Math.Floor(decAMT / 0.1D)
            decAMT -= dec10 * 0.1D
            dec5 = Math.Floor(decAMT / 0.05D)
            decAMT -= dec5 * 0.05D
            dec1 = Math.Floor(decAMT / 0.01D)
            decAMT -= dec1 * 0.01D
            Debug.WriteLine("Pennies " & dec1) 'should be 3
            Debug.WriteLine("Nickles " & dec5)
            Debug.WriteLine("Dimes " & dec10)
            Debug.WriteLine("Quarters " & dec25)
            Debug.WriteLine(decAMT) 'should be 0

  5. #5
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Displaying the wrong results

    Quote Originally Posted by Oblio View Post
    Code:
            Debug.WriteLine("Calculate Change")
            Dim decAMT As Decimal = 0.73D 'change
            Dim dec25, dec10, dec5, dec1 As Decimal 'quarters, dimes,nickles, pennies
            'calculate change for amount using Decimal
            dec25 = Math.Floor(decAMT / 0.25D)
            decAMT -= dec25 * 0.25D
            dec10 = Math.Floor(decAMT / 0.1D)
            decAMT -= dec10 * 0.1D
            dec5 = Math.Floor(decAMT / 0.05D)
            decAMT -= dec5 * 0.05D
            dec1 = Math.Floor(decAMT / 0.01D)
            decAMT -= dec1 * 0.01D
            Debug.WriteLine("Pennies " & dec1) 'should be 3
            Debug.WriteLine("Nickles " & dec5)
            Debug.WriteLine("Dimes " & dec10)
            Debug.WriteLine("Quarters " & dec25)
            Debug.WriteLine(decAMT) 'should be 0
    I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?).

    It was looking simpler to do a class with it, but in the end, it turned more complex:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim MyCoinChange As New CoinChange
    
            For Each CoinName As String In MyCoinChange.GetChange(15.16).Keys
                Debug.WriteLine(CoinName.PadRight(9) & ":" & MyCoinChange.GetChange()(CoinName))
            Next CoinName
        End Sub
    
        Private Class CoinChange
            Friend CoinsValue As New Dictionary(Of String, Double)
    
            Friend Sub New()
                With CoinsValue
                    .Add("Quarters", 25 / 100)
                    .Add("Dimes", 10 / 100)
                    .Add("Nickles", 5 / 100)
                    .Add("Pennies", 1 / 100)
                End With
            End Sub
    
            Friend ReadOnly Property GetChange(Optional ByVal Mount As Double = Double.NaN) As Dictionary(Of String, Double)
                Get
                    Static Answer As Dictionary(Of String, Double)
    
                    If Double.IsNaN(Mount) Then
                        Return Answer
                    Else
                        Answer = New Dictionary(Of String, Double)
                    End If
    
    
                    For Each CoinName As String In CoinsValue.Keys
                        Dim CoinCount As Integer
    
                        CoinCount = CInt(Math.Floor(Mount / CoinsValue(CoinName)))
                        Answer.Add(CoinName, CoinCount)
                        Mount -= CoinCount * CoinsValue(CoinName)
                    Next CoinName
    
                    Return Answer
    
                End Get
            End Property 'GetChange
        End Class 'CoinChange
    
    End Class
    The class can also count the remaining coins in the store.
    Last edited by Marraco; January 4th, 2010 at 12:25 PM. Reason: changet double.minvalue to double.Not a Number
    [Vb.NET 2008 (ex Express)]

  6. #6
    Join Date
    Apr 2008
    Posts
    82

    Re: Displaying the wrong results

    "I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?)."

    That is Integer division so things like 0.05D are getting rounded to ... 0

  7. #7
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Displaying the wrong results

    Quote Originally Posted by Oblio View Post
    "I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?)."

    That is Integer division so things like 0.05D are getting rounded to ... 0
    Oh. I see.

    I were thinking that on this age, any x86 processor contains the "\" operator supported in hardware for any floating point format...

    There are so many useless instructions...
    [Vb.NET 2008 (ex Express)]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured