13 Replies - 747 Views - Last Post: 20 October 2013 - 10:08 AM Rate Topic: -----

#1 Naomi30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-September 13

Division By Zero

Posted 19 October 2013 - 06:33 PM

Hey all

Im having trouble catching an error when the user tries to divide by 0


Dim intFirstNumber As Integer
        Dim intSecNumber As Integer
        Dim intTotal As Integer



        Integer.TryParse(txtNum1.Text, intFirstNumber)
        Integer.TryParse(txtNum2.Text, intSecNumber)


        intTotal = intFirstNumber \ intSecNumber
        lblQuotient.Text = intTotal.ToString

        If intFirstNumber > intSecNumber Then
            intTotal = intFirstNumber \ intSecNumber
            lblQuotient.Text = intTotal.ToString
        End If


            If intSecNumber = 0 Then
                intTotal = MsgBox(" Cannot divide by 0")
            End If

This post has been edited by andrewsw: 19 October 2013 - 06:53 PM
Reason for edit:: Use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Division By Zero

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Division By Zero

Posted 19 October 2013 - 06:55 PM

Please wrap your code in code-tags, it tells you how to do this directly in the area where you are typing your code.

Quote

Im having trouble catching an error when the user tries to divide by 0

What trouble are you having? Your code above doesn't show any attempt to catch this error. (You do check if the second value is 0 at the bottom, but by then it's too late.)

Try...Catch...Finally Statement :MSDN

This post has been edited by andrewsw: 19 October 2013 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 670
  • View blog
  • Posts: 2,946
  • Joined: 03-December 12

Re: Division By Zero

Posted 19 October 2013 - 06:56 PM

You need to move your check if 0 statement before you try to do anything with it. Right now it is at the bottom.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Division By Zero

Posted 19 October 2013 - 07:02 PM

View Postastonecipher, on 20 October 2013 - 01:56 AM, said:

You need to move your check if 0 statement before you try to do anything with it. Right now it is at the bottom.

Quite right ;). I was assuming the OP is supposed to be studying error handling. If not, then moving this statement is obviously the simplest solution.
Was This Post Helpful? 0
  • +
  • -

#5 Naomi30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-September 13

Re: Division By Zero

Posted 19 October 2013 - 07:26 PM

I moved the error but it still crashes
Was This Post Helpful? 0
  • +
  • -

#6 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Division By Zero

Posted 19 October 2013 - 07:27 PM

View PostNaomi30, on 19 October 2013 - 07:26 PM, said:

I moved the error but it still crashes


You moved the error? Specify that better posting exactly what you moved.
Was This Post Helpful? 0
  • +
  • -

#7 Naomi30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-September 13

Re: Division By Zero

Posted 19 October 2013 - 07:29 PM

Dim intFirstNumber As Integer
        Dim intSecNumber As Integer
        Dim intTotal As Integer



        Integer.TryParse(txtNum1.Text, intFirstNumber)
        Integer.TryParse(txtNum2.Text, intSecNumber)

        If intFirstNumber > intSecNumber Then
            intTotal = intFirstNumber \ intSecNumber
            lblQuotient.Text = intTotal.ToString
        End If


        If intSecNumber = 0 Then
            intTotal = MsgBox(" Cannot divide by 0")
        End If


        intTotal = intFirstNumber \ intSecNumber
        lblQuotient.Text = intTotal.ToString

This post has been edited by andrewsw: 19 October 2013 - 07:30 PM
Reason for edit:: USE CODE TAGS

Was This Post Helpful? 0
  • +
  • -

#8 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Division By Zero

Posted 19 October 2013 - 07:33 PM

Post it in code tags please, and in future post the line of the error, and what is says.

Edit: too late...

why are you doing this twice?
intTotal = intFirstNumber \ intSecNumber
        lblQuotient.Text = intTotal.ToString



You should put this

If intSecNumber = 0 Then
intTotal = MsgBox(" Cannot divide by 0")
End If


Before anything else as suggested above, and if it equals 0, you do

If intSecNumber = 0 Then
intTotal = MsgBox(" Cannot divide by 0")
Exit Sub
Else
'Your calculation here.
End If


This post has been edited by HeBTeMy: 19 October 2013 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 670
  • View blog
  • Posts: 2,946
  • Joined: 03-December 12

Re: Division By Zero

Posted 19 October 2013 - 07:37 PM

Your statement. Needs to be along the lines of:


If (this =0) then
' handle the input
Else
' continue calculation
End if 


Was This Post Helpful? 0
  • +
  • -

#10 Naomi30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-September 13

Re: Division By Zero

Posted 19 October 2013 - 07:51 PM

the whole idea of this program is two divide the larger number by the smaller number. then I need to catch the error where the user can not divide by zero. I have the part working where the larger number divides into the smaller number.
Was This Post Helpful? 0
  • +
  • -

#11 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Division By Zero

Posted 19 October 2013 - 08:01 PM

View PostNaomi30, on 19 October 2013 - 07:51 PM, said:

the whole idea of this program is two divide the larger number by the smaller number. then I need to catch the error where the user can not divide by zero. I have the part working where the larger number divides into the smaller number.


So, what's the problem?
The code above should work for what you're trying to accomplish, if the secondnumber which you declared as the dividend is equal to 0 then you Exit sub, Else, you proceed... am i right?
Was This Post Helpful? 0
  • +
  • -

#12 Naomi30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-September 13

Re: Division By Zero

Posted 19 October 2013 - 08:31 PM

when I divide by 0 the application crashes
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 670
  • View blog
  • Posts: 2,946
  • Joined: 03-December 12

Re: Division By Zero

Posted 19 October 2013 - 08:44 PM

You can't divide by 0. How to handle if 0 has been entered as a divisor has already been provided. So, what issue are you having?
Was This Post Helpful? 0
  • +
  • -

#14 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 150
  • Joined: 22-September 09

Re: Division By Zero

Posted 20 October 2013 - 10:08 AM

Try Andy's suggestion:

  Try
            intTotal = CInt(1 / intSecNumber) 'divide by zero
        Catch
            MsgBox("Whatever cannot divide by 0. Please try again", MsgBoxStyle.Critical, "Error")
            'End sub
        End Try


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1