Results 1 to 5 of 5

Thread: Coin change program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    18

    Post

    I have to make a program that tells you what change you get back in quarters, dimes and nickel, and pennies. From a value below a dollar. I just started programming in visual basic and this program is due Friday and I have no clue what to do using the Mod division thing. This is what I have so far but doesn't work of course:

    Dim inttxtinputchange As Integer
    Dim intquarters As Integer
    Dim intdimes As Integer
    Dim intnickels As Integer
    Dim intpennies As Integer
    inttxtinputchange = txtinputchange.Text
    lblquarters.Caption = inttxtinputchange Mod 25
    lbldimes.Caption = inttxtinputchange Mod 10
    lblnickels.Caption = inttxtinputchange Mod 5
    lblpennies.Caption = inttxtinputchange Mod 1

    Can someone please give me the correct code or at least help me out?

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196

    Post

    Your problem with Mod is that it returns the remainder (ie 11 mod 4 = 3) not the quotient (11 quotient 4 = 2; 4 goes into 10 2 times). Below is some code that will do what you want. Its not the prettiest little ditty but it does the job.

    As of yet i have not found a quotiend operator in vb so you use the Int() function.

    Hope this helps - don't let your teacher see this message...

    ----------------------------------------

    Dim inttxtinputchange As Integer
    Dim intquarters As Integer
    Dim intdimes As Integer
    Dim intnickels As Integer
    Dim intpennies As Integer
    Dim temp As Integer
    inttxtinputchange = txtinputchange.Text


    'Find out how many quarters
    intquarters = Int(inttxtinputchange / 25)

    'Find out how much is left after the quarters
    temp = inttxtinputchange - intquarters * 25

    'Find out how many dimes
    intdimes = Int(temp / 10)

    'find out how much is left after the dimes
    temp = temp - intdimes * 10

    'find out how many nickels
    intnickels = Int(temp / 5)

    'find out how much is left after the nickels
    'this will be the number of pennies
    temp = temp - intnickels * 5
    intpennies = temp

    lblquarters.Caption = intquarters
    lbldimes.Caption = intdimes
    lblnickels.Caption = intnickels
    lblpennies.Caption = intpennies

  3. #3
    Addicted Member
    Join Date
    Sep 1999
    Posts
    229

    Post




    Hi Marc,

    You can try this code.


    Dim intQ As Integer
    Dim intD As Integer
    Dim intN As Integer
    Dim intP As Integer
    Dim intC As Integer 'cent

    Dim dblChange As Double

    Dim strMsg As String

    dblChange = 141.25 'for example Me.txtChange

    intQ = dblChange \ 25
    If (intQ > 0) Then
    dblChange = dblChange - (intQ * 25)
    Else
    intQ = 0
    End If

    intD = dblChange \ 10
    If (intD > 0) Then
    dblChange = dblChange - (intD * 10)
    Else
    intD = 0
    End If

    intN = dblChange \ 5
    If (intN > 0) Then
    dblChange = dblChange - (intN * 5)
    Else
    intN = 0
    End If

    intP = dblChange \ 1
    If (intP > 0) Then
    dblChange = dblChange - (intP * 1)
    Else
    intP = 0
    End If

    intC = Int(dblChange * 100)

    strMsg = strMsg & "Q = " & Str(intQ) & Chr$(13)
    strMsg = strMsg & "D = " & Str(intD) & Chr$(13)
    strMsg = strMsg & "N = " & Str(intN) & Chr$(13)
    strMsg = strMsg & "P = " & Str(intP) & Chr$(13)
    strMsg = strMsg & "C = " & Str(intC)

    MsgBox (strMsg)

    Does it help ?

    Regards
    Keiko

  4. #4
    Addicted Member
    Join Date
    Jan 1999
    Posts
    239

    Post

    Don't want to do your homework for you, but
    here is a start, the MOD Operator gives the
    Remainder in a division, i.e.,

    100 divided by 12 = 8 with a Remainder of 4

    so 100 MOD 12 will give an answer of 4

    'Put the following in a form's click event
    Code:
    AmtLeft= 100-44 'spent 44 cents
    amt = 56 / 25 'how many quarters
    'strip off the digits after decimal
    Print "amt = "; Format(amt, "##") & " equals number of Quarters"
    
    'how many cents left - the Mod Operator will
    'tell how many cents are left
    amt1 = 56 Mod 25
    Print
    Print "amt1 = "; amt1; "equals amount of cents left"

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    18

    Post

    thank you so much


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