1 Replies - 1971 Views - Last Post: 20 February 2010 - 02:29 PM Rate Topic: -----

#1 JanetB2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-February 10

Pennies ChangeCalculator

Posted 20 February 2010 - 02:00 PM

Here's my code --- please advise what I'm doing wrong or missing.

Pennies cashed in at bank i.e. 100 pennies should return Dollars $1, Quarters 0, Nickels, 0 etc.

'Name:          Pennies Calculator
'Purpose:       Calculate and display change received from pennies in a jar
'Programmer:    Janet B on 2/3/2010

Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'Closes application upon clicking exit
        Me.Close()

    End Sub

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        
        'Calculates change value from number of pennies cashed in at the bank

        'Declare variables
        Dim intTotalPennies As Integer
        Dim decTOTALPENNIES As Decimal
        Dim decDollars As Decimal
        Dim decQuarters As Decimal
        Dim decDimes As Decimal
        Dim decNickels As Decimal
        Dim decPennies As Decimal


        Integer.TryParse(txtTotalPennies.Text, intTotalPennies)
        intTotalPennies = CInt(decDollars)


        'calculates and displays correct change value
        Decimal.TryParse(CStr(intTotalPennies), decTOTALPENNIES)
        decDollars = CDec((intTotalPennies / 100))
        decQuarters = CDec((intTotalPennies / 25))
        decDimes = CDec((intTotalPennies / 10))
        decNickels = CDec((intTotalPennies / 5))
        decPennies = CDec((intTotalPennies / 1))

        lblDollars.Text = CStr(decDollars)
        lblQuarters.Text = CStr(decQuarters)
        lblDimes.Text = CStr(decDimes)
        lblNickels.Text = CStr(decNickels)
        lblPennies.Text = CStr(decPennies)


    End Sub

    Private Sub ClearLabels(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTotalPennies.TextChanged

        'clears total pennies, dollars, quarters, dimes, nickels, pennies

        lblDollars.Text = ""
        lblQuarters.Text = ""
        lblDimes.Text = ""
        lblNickels.Text = ""
        lblPennies.Text = ""

    End Sub

End Class



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Pennies ChangeCalculator

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Pennies ChangeCalculator

Posted 20 February 2010 - 02:29 PM

The main problem is that you're not removing the previous monetary denomination from your intTotalPennies variable.

If you have 123 pennies, you have 1 dollar and 23 pennies are left, so you have 23/25 = < 1 so 0 quarters
so then you have 23/10 and you get 2 so you have 2 dimes and 3 pennies etc...

You are taking 123 pennies and dividing by 100 and get 1 dollar
then you are taking those same 123 pennies and dividing by 25 and getting 4 quarters all the way to pennies where you will end up with 123 pennies.

After you determine the number of dollars, remove that amount from the calculation and use the result for the next denomination.

EDIT:
One other thing I forgot to mention is this line
intTotalPennies = CInt(decDollars)
You are setting intTotalPennies to 0 at this point so your textbox entry isn't being used anymore after that.

This post has been edited by CharlieMay: 20 February 2010 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1