Newbie needs help with homework how can I return change in numbers or dollars, quarters etc?

  • Thread starter Thread starter Amy
  • Start date Start date
A

Amy

Newbie needs help with homework how can I return change in numbers or
dollars, quarters etc?

This question is so simple I am embarrassed to put it here but I have only
been coding for 1 month and am totally stumped!!
My homework requires me to make a program that calculates change returned
and then displays how many dollars, quarters, dimes, nickels, and pennies to
give back for the change. like $2.32 in change is 2 dollars 3 dimes and 2
pennies.
I don't know if I am stupid in math or coding or both but I can not make
this work. can someone help?

Private Sub CalculateChangeButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CalculateChangeButton.Click

'Declare variables

Dim decAmountOwed, decAmountPaid, decChangeOwed As Decimal

Dim sngDollarsOwed, sngQuartersOwed, sngDimesOwed, sngNicklesOwed,
sngPenniesOwed, _

sngChangeChange As Single

'Assign values to variables

decAmountOwed = Val(Me.AmountOwedTextBox.Text)

decAmountPaid = Val(Me.AmountPaidTextBox.Text)

sngDollarsOwed = Val(Me.DollarsOutputLabel.Text)

sngQuartersOwed = Val(Me.QuartersOutputlabel.Text)

sngDimesOwed = Val(Me.DimesOutputlabel.Text)

sngNicklesOwed = Val(Me.NicklesOutputLabel.Text)

sngPenniesOwed = Val(Me.PenniesOutputlabel.Text)

'Perform Calculations

'Calculate and display the change owed

decChangeOwed = decAmountPaid - decAmountOwed

Me.ChangeOwedOutputLabel.Text = Format(decChangeOwed, "currency")



'Calculate and display the dollars owed

sngDollarsOwed = Val(decChangeOwed) Mod 1

Me.DollarsOutputLabel.Text = sngDollarsOwed



'Calculate and display the number of Quarters owed

'Calculate and display the number of Dimes owed

'Calculate and display the number of Nickles owed

'Calculate and display the number of Pennies owed



'Display formatted results in the controls

Me.AmountOwedTextBox.Text = Format(decAmountOwed, "currency")

Me.AmountPaidTextBox.Text = Format(decAmountPaid, "currency")

'Send the focus to the Amount Owed box

Me.ClearScreenButton.Focus()

End Sub



End Class
 
If the change is 2.32, first convert to pennies by multiplying by 100.

If you divide by 100 (integer division) you get the number of dollars -- in
this case 2.

Subtract the answer * 100 from the number of pennies.

Try the next larger (quarters)...

Divide by 25 (you get 1 in this case) then subtract the answer * 25 from
the pennies (in this case leaving 7)

Divide by 10 (dimes) then subtract 10 * answer

Divide by 5 (nickels) then subtract 5 * answer

You are left with pennies.

That help?

By the way, your 2 dollars, 3 dimes and two pennies would not occur. 2
dollars, 1 quarter, one nickel, 2 pennies would.
 
Amy:

OT answered your question, but let me offer a few suggestions:

1) The crux of this problem is the algorithm you use...so once you have
that, the battle is pretty much over and Ot's algorithm will get you there.
Don't be discouraged ;-)
2) Get rid of the use of Val. IMHO, using Val in .NET is a crime against
humanity, you can use Integer.Parse(textBox.Text) or
decimal.Parse(textBox.Text). There are many reasons this is preferable, but
one of the more obvious ones is that if you use Val, and I put in
MyNameIsBill in the textbox, that's perfectly legal. You'll want to raise
an exception, or better yet, ensure that only numbers can be entered into
the text box (and perhaps a Dollar Sign [assuming you're here in the States]
and a decimal) You can trap the key events and use e.Handled = true if you
get something that isn't kosher. From my experience, adding small
enhancements like this will help you to distinguish yourself in the
professor's eyes which is usually a good thing
3)I'm not sure how far you are in programming, but you'll want to make a
function out of it and if you're familiar with recursion, this would
probably be a decent place to implement it.

Run with OT's algorithm and if you get stuck, let me know.

cheers,

Bill



Amy said:
Newbie needs help with homework how can I return change in numbers or
dollars, quarters etc?

This question is so simple I am embarrassed to put it here but I have
onlybut may I make a suggestion about coding style?
 
Hi, I keep trying to reply to the group in outlook express but it says it
can't find the group or something. Thank you for your suggestions. I am glad
to know that using Val is a crime in VB.Net! :) Unfortunately I must have
not learned anything that you discuss here yet because none of it looks
familiar. However I am only on chapter 4 in the book. Frankly, I was not
sure why I had to use the Val at all but couldn't figure out the easy as pie
algorithm so I wasn't to concerned with that just yet. Thank you for your
suggestion!
 
Back
Top