Results 1 to 23 of 23

Thread: Change Machine

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Change Machine

    Visual Basic - Change Machine?

    I am a student at Washington State University and need help with a Change Machine...

    Ok How do I make my program execute change. For example, if I have 3 text boxes with Amount of Purchase, Amount Entered, and Change Due. And then 9 check boxes with 20,10,5,1, Dollars,Dimes,Nickels, Pennies and a label box corresponding to each one, and a Calculate Button,how do I make the calculate button correspond with the label boxes corresponding to the number. For example, If I put 20 in for amount of purchase, and 30 for Amount Entered, and the Change Due should be 10. And then in there should be the number one appear in the box next to $10.

    I just need help writing the beggining of the code and where at. Then I can take it from there

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    Since this is an exercise in analysis and not just writing code I'll give you a narrative explanation rather than spoonfeed you with code.

    If checkbox for 20 is checked then get number of bills through integral division. You then update change using modular division to get remaining change due less 20 bills. You repeat the process for the rest of denominations.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Ok Thanks, but can you like just show me how it would look Code wise? i am not asking to be fed the code, I just am a newbie and have no clue what to type it in as. But wouldnt it be \2000 and not \20?

    So would it look like

    For Private Sub BtnCalculate Click(....
    If Chktwenty.Checked=False then
    ---->intTwenty=intchange\2000
    ---->intChange=intChangeMod2000
    lblTwenty=intTwenty

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    Yup, you have to work in cents (also adjust change due to cents) cause integral and modular division only works with integers (no decimals), glad you figured that out yourself. See, its an exercise in analysis too and not just writing code. If you didn't adjust to cents then you'd get wrong values; it would then be an opportunity to learn debugging. Those are more important in the long run IMO.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Change Machine

    http://www.vbforums.com/showthread.php?t=510765
    There's absolutely no point in duplicating questions as it just causes confusion, especialy if you half remove one of them. Now you have 1 and a half threads both with replies.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Quote Originally Posted by leinad31
    Yup, you have to work in cents (also adjust change due to cents) cause integral and modular division only works with integers (no decimals), glad you figured that out yourself. See, its an exercise in analysis too and not just writing code. If you didn't adjust to cents then you'd get wrong values; it would then be an opportunity to learn debugging. Those are more important in the long run IMO.

    Ok wait wait, so then is that put under the btnCalculate section?

    So do I put that under 'declare local variable/'import interface/'calculations/'display results?

    and when I type in int into the code it says error. Where is the Int in the design?

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    You have to declare your variables accordingly with correct scope.

    As to the button, its a single transaction/process isn't it?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Quote Originally Posted by leinad31
    You have to declare your variables accordingly with correct scope.

    As to the button, its a single transaction/process isn't it?

    ok i double click the calculate button and the beggining of the code appears.

    I have no clue what to do now, as in How do I make it calculate the cash tendered, amount due and change due into how many 20s,10s,5s,1s, dimes,nickels, pennies, and quarters are given back in the label boxes next to them.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    You just repeat the process, only the starting value changes.

    If you started with 35 then less one 20 your left with 15. You then repeat process starting with 15 giving you one 10 with 5 left. And so on and so forth.

  10. #10
    Addicted Member arunb's Avatar
    Join Date
    Jul 2005
    Posts
    131

    Re: Change Machine

    For Private Sub BtnCalculate Click(....
    If Chktwenty.Checked=False then
    ---->intTwenty=intchange\2000
    ---->intChange=intChangeMod2000
    lblTwenty=intTwenty
    What's the ----> for ??

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Ok so Would my full code look like this....

    For Private Sub BtnCalculate Click(....

    'calculations
    If Chktwenty.Checked=False then
    intTwenty=intchange\2000
    intChange=intChangeMod2000
    lblTwenty=intTwenty

    If ChkTen.Checked=False then
    inttenn=intchange\1000
    intChange=intchangeMod1000
    lblTen=intTen

    If ChkFive.Checked=False then
    intfive=intchange\500
    intChange=intchangeMod500
    lblFive=intFive

    If ChkOne.Checked=False then
    intone-intchange\100
    intChange=intChangeMod100
    lblOne=intOne

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Anyone?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Leinad31???

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change Machine

    Please do not bump threads. For some of the reasons, see this

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    Quote Originally Posted by DarkFalcon
    Leinad31???
    Well, have you tried running it? That's often faster than waiting for a reply. Did you get the expected output? What if none of the checkboxes were checked or there is still change left over? Where will you place the amount still left over?

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Quote Originally Posted by leinad31
    Well, have you tried running it? That's often faster than waiting for a reply. Did you get the expected output? What if none of the checkboxes were checked or there is still change left over? Where will you place the amount still left over?
    Ok Its not working, because the word intchange is underlined in a blue squiggly. SO how do I make it unsquiggly?

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    As mentioned in post #7, did you declare your variables properly? Type Option Explicit as the very first line in each of your form/code modules. That will help you trace such errors.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Quote Originally Posted by leinad31
    As mentioned in post #7, did you declare your variables properly? Type Option Explicit as the very first line in each of your form/code modules. That will help you trace such errors.

    Ok, What do I need to delcare, that I am unsure of....

  19. #19
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    For variable declarations with scope limited to within procedure, something like...

    Code:
    Option Explicit
    
    Private Sub BtnCalculate Click()
       Dim intTwenty As Integer  
    End Sub

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Change Machine

    Quote Originally Posted by leinad31
    For variable declarations with scope limited to within procedure, something like...

    Code:
    Option Explicit
    
    Private Sub BtnCalculate Click()
       Dim intTwenty As Integer  
    End Sub

    But intTwenty isnta variable on the interface though? It says unused local variable for intTwenty when I typed that into the code

  21. #21
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    Yes, what you see on form are called controls. That code simply declared a variable, which is not a control on the form. Here is a sample that uses (a value was assigned) the variable you declares.
    Code:
    Option Explicit
    
    Private Sub BtnCalculate Click()
       Dim intTwenty As Integer  
       intTwenty = 20
       MsgBox intTwenty
       intTwenty = 2008 /2 
       MsgBox intTwenty
    End Sub

  22. #22
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Change Machine

    leinad31: before you get too involved, you need to understand that Op is using .NET - see: http://www.vbforums.com/showthread.php?t=510926

  23. #23
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change Machine

    Question was posted in VB6 section. If he's using .Net then update code accordingly. Algorithm concept stays the same.

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