Wrong results displayed

jurdan01

New member
Joined
Dec 12, 2009
Messages
3
Programming Experience
Beginner
Hi
I have attempted this code for an application that calculates and displays change in terms of quarters, dimes, nickels and pennies. The user inserts the amount paid and the cost of the item both expressed in cents in two textboxes. Then I have four labels displaying the results for each kind of coin. This is the process for the button I named Calculate:

VB.NET:
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

The results are wrong whenever I try to type some numbers. I can't figure out where I'm going wrong... I need to finish this tonight. If someone can help me out, I'd be very grateful. I don't know how to correct the code... I'm not really good at programming, just starting:(.
 
You should be using the \ (integer division) and Mod operators for these calculations. You use integer division to calculate the number of a specific denomination and then you use Mod to calculate the amount left over to pass on to the next calculation.
 
You should be using the \ (integer division) and Mod operators for these calculations. You use integer division to calculate the number of a specific denomination and then you use Mod to calculate the amount left over to pass on to the next calculation.

Ok so... I modified the code to use integer division. But in this case, how do I use MOD? Could you just give me one example?
...I am sorry to bother you but, as I said, I am not really strong at this.

Edit: I noticed that by using the integer division, the results displayed are correct. Only, there's a minus sign before the numbers.
:confused:
 
Last edited:
Here is something similar:
VB.NET:
Dim Qcount As Integer 
Dim Dcount As Integer 
Dim Ncount As Integer 
Dim Pcount As Integer 
Private Sub GetLowestCoinNumber(ByVal amt As Integer)
  Do 
      Select Case amt
          Case 25 To 99
               Qcount += 1
               amt -= 25
          Case 10 To 24
               Dcount += 1
               amt -= 10
          Case 5 To 9
               Ncount += 1
               amt -= 5
          Case 1 To 4
               Pcount += 1
               amt -= 1
      End Select
  Loop Until amt = 0
  Me.lblQAmount.Text = Qcount.ToString
  Me.lblDAmount.Text = Dcount.ToString
  Me.lblNAmount.Text = Ncount.ToString
  Me.lblPAmount.Text = Pcount.ToString
Qcount = 0 : Dcount = 0 : Ncount = 0 : Pcount = 0
End Sub
'call the sub to calculate the value
GetLowestCoinNumber(Convert.ToInt32(txtAmt.Text))
 
Mod is a binary operator, just like integer division, e.g.
VB.NET:
change = change Mod centsPerDollar
As is always the case, if you want to know how to use something in .NET then the first thing you should do is read the relevant documentation.

mod - MSDN Search
 
{Be hAppy}

{Be hAppy}

Private Sub Test()
MsgBox Distribution(97)
End Sub


Private Function Distribution(ByVal Data As Integer) As String
Return "Q=" & GetQuantity(Data, 25) & " D=" & _
GetQuantity(Data, 10) & " N=" & GetQuantity(Data, 5) & " P=" & Data
End Function


Private Function GetQuantity(ByRef Data As Integer, ByVal Divider As Integer)
Dim Result = Data \ Divider
Data -= Result * Divider
Return Result
End Function
 

Latest posts

Back
Top