6 Replies - 2799 Views - Last Post: 20 February 2012 - 10:51 AM Rate Topic: -----

#1 Nuclearf1sh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-February 12

Error: Attempted to divide by zero.

Posted 19 February 2012 - 05:02 PM

I'm writing a program that returns the change after the user inputs the money given and purchase amount. It will show how many dollars, quarters, dimes, nickels, and pennies a customer would get back.

Posted Image

I get an error after entering the numbers though, "Attempted to divide by zero". What am I doing wrong and how do I fix this?

Forgot to post my code =/

Dim decAmountGiven As Decimal
        Dim decPurchaseAmount As Decimal
        Dim change As Decimal

        If String.IsNullOrEmpty(txtAmountGiven.Text) Or String.IsNullOrEmpty(txtPurchaseAmount.Text) Then
            MsgBox("Please enter a number")
        ElseIf txtAmountGiven.Text < txtPurchaseAmount.Text Then
            MsgBox("You have not given enough money")
        Else
            decAmountGiven = txtAmountGiven.Text
            decPurchaseAmount = txtPurchaseAmount.Text

            decAmountGiven = txtAmountGiven.Text
            decPurchaseAmount = txtPurchaseAmount.Text

            change = (decAmountGiven - decPurchaseAmount)
            lblMoneyReturn.Text = change

            lblDollarsOutput.Text = change \ 1
            change = change Mod 1
            lblQuartersOutput.Text = change \ 0.25
            change = change Mod 0.25
            lblDimesOutput.Text = change \ 0.1
            change = change Mod 0.1
            lblNickelsOutput.Text = change \ 0.05
            change = change Mod 0.05
            lblPenniesOutput.Text = change
        End If



Is This A Good Question/Topic? 0
  • +

Replies To: Error: Attempted to divide by zero.

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Error: Attempted to divide by zero.

Posted 19 February 2012 - 05:22 PM

Try to change those \ with / because \ is an integer devider.
Read this post here
Was This Post Helpful? 1
  • +
  • -

#3 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Error: Attempted to divide by zero.

Posted 19 February 2012 - 06:14 PM

To be technical, WTH would you use a back slash (\) for division? Even basic logic describes the 4 basic operators for math (+, -, *, /)

Make sure you syntax check your code, since you had a rampant case of backslash (\) through you math.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error: Attempted to divide by zero.

Posted 19 February 2012 - 06:30 PM

@woodjom the back slash is used for integer division. 3/2 = 1.5 but 3\2 = 1. But I agree with smohd this is not the result wanted when using the remainder for the remaining coins.

Check this out, it's another topic based on this. Might help you through it.

This post has been edited by CharlieMay: 19 February 2012 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Error: Attempted to divide by zero.

Posted 20 February 2012 - 04:48 AM

guess ive always used the MOD operator to gain the operand, or let the value of Int be rounded based on the remainder.
Was This Post Helpful? 0
  • +
  • -

#6 Nuclearf1sh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-February 12

Re: Error: Attempted to divide by zero.

Posted 20 February 2012 - 10:35 AM

Thanks CharlieMay found some good stuff in that link.

I ended up just getting rid of all the division.

            Do While change <> 0
                Select Case change
                    Case Is >= 1
                        dollars = dollars + 1
                        change = change - 1
                    Case Is >= 0.25
                        quarters = quarters + 1
                        change = change - 0.25
                    Case Is >= 0.1
                        dimes = dimes + 1
                        change = change - 0.1
                    Case Is >= 0.05
                        nickels = nickels + 1
                        change = change - 0.05
                    Case Is >= 0.01
                        pennies = pennies + 1
                        change = change - 0.01
                End Select
            loop

        lblDollarsOutput.Text = dollars
        lblQuartersOutput.Text = quarters
        lblDimesOutput.Text = dimes
        lblNickelsOutput.Text = nickels
        lblPenniesOutput.Text = pennies


Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Error: Attempted to divide by zero.

Posted 20 February 2012 - 10:51 AM

Now do the same problem this time with using division.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1