Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Visual Basic Change Calculator (Using Visual Studio 2010) NEED HELP!!!?

I'm just learning how to program in Visual Basic. I have to create a change calculator.

Input = total amount of the order and money received from customer

output = change. (The change has to be broken down in dollars, quarters, nickles, dimes, and pennies)

I keep having problems with the number of dollars rounding up.(i.e change = 4.75, the dollars owed comes out to 5) is there anyway to stop this? I tried Single and Decimal. I might not have been using them correctly though. I really appreciate anyone who helps me.

code:

Private Sub btnCalculateChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateChange.Click

Dim change As Double

Dim totalAmount As Double

Dim moneyReceived As Double

Dim dollarsOwed As Double

Dim quartersOwed As Double

Dim dimesOwed As Double

Dim nicklesOwed As Double

Dim penniesOwed As Double

Const dollarValue As Double = 1.0

Const quarterValue As Double = 0.25

Const dimeValue As Double = 0.1

Const nickleValue As Double = 0.05

Const pennyValue As Double = 0.01

totalAmount = CDbl(txtTotalAmount.Text)

moneyReceived = CDbl(txtMoneyReceived.Text)

change = moneyReceived - totalAmount

txtChange.Text = CStr(change)

txtChange.Text = Format(change, "0.00")

dollarsOwed = CInt(change / dollarValue)

change = change - (dollarsOwed * dollarValue)

txtDollarsOwed.Text = CStr(dollarsOwed)

quartersOwed = CInt(change / quarterValue)

change = change - (quartersOwed * quarterValue)

If quartersOwed < 0 Then quartersOwed = quartersOwed * -1

txtQuartersOwed.Text = CStr(quartersOwed)

dimesOwed = CInt(change / dimeValue)

change = change - (dimesOwed * dimeValue)

txtDimesOwed.Text = CStr(dimesOwed)

nicklesOwed = CInt(change / nickleValue)

change = change - (nicklesOwed * nickleValue)

txtNicklesOwed.Text = CStr(nicklesOwed)

penniesOwed = CInt(change / pennyValue)

change = change - (penniesOwed * pennyValue)

txtPenniesOwed.Text = CStr(penniesOwed)

End Sub

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Why not use the following code. It's much cleaner and most of all it's correct.

    There are 2 buttons, 2 text boxes and 6 labels on the form.

    Button1 does the calculation

    Button2 clears the data for another calculation

    Label1 indicates the dollars in change

    Label2 indicates the quarters in change

    Label3 indicates the dimes in change

    Label4 indicates the nickles in change

    Label5 indicates the pennies in change

    Label6 indicates the total change

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Penny, Nickle, Dime, Quarter, Dollar As Integer

    Dim Purchase, Tendered, Total As Double

    Dim Remaining1, Remaining2, Remaining3 As Integer

    Purchase = Val(TextBox1.Text)

    Tendered = Val(TextBox2.Text)

    Total = (Tendered - Purchase) * 100

    Dollar = Total \ 100

    Label1.Text = Dollar

    Remaining1 = Total Mod 100

    Quarter = Remaining1 \ 25

    Label2.Text = Quarter

    Remaining2 = Remaining1 Mod 25

    Dime = Remaining2 \ 10

    Label3.Text = Dime

    Remaining3 = Remaining2 Mod 10

    Nickle = Remaining3 \ 5

    Label4.Text = Nickle

    Penny = Remaining3 Mod 5

    Label5.Text = Penny

    Label6.Text = Total / 100.ToString("#0.00")

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    TextBox1.Text = ""

    TextBox2.Text = ""

    Label1.Text = ""

    Label2.Text = ""

    Label3.Text = ""

    Label4.Text = ""

    Label5.Text = ""

    Label6.Text = ""

    End Sub

    End Class

    Texmav

  • Anonymous
    9 years ago

    I havent read your program but let me try and help. It's a little to much code for me to read. But I think I can still help solve your problem. This is what you do.

    First of I would create a seperate method, then call that method when the button is clicked. If I were you I would have two methods actually. One in which you calculate the total price and the other which you calculate the change and display all information. Your code is a little hard to read and this will make it easier for someone to help you. Now you said you have a problem with it rounding? I'm just taking a guess here because again I didn't look at your code, but could you be turning a double to an int anywhere. If you are turning a double to an int it will cause things to round. Int is for whole numbers and Doubles are for decimals. Make sure You check all your casts and check to see that your not changing a double to an int any where in your code.

    Source(s): me :3
  • Anonymous
    5 years ago

    And that's the downside of learning using console instead of a form. Basically, take all those writelines and make them labels and then add textboxes for the inputs.

Still have questions? Get your answers by asking now.