Results 1 to 10 of 10

Thread: "Negative" Probelm...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    115

    "Negative" Probelm...

    I'm just a beginner in VB and I made a program which says if a number is a perfect square or not. When I enter a negative number I get an error. How can I fix this problem?

    Thanks.

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: "Negative" Probelm...

    Don't allow the user to enter negative numbers. Check the number before you calculate whether it is negative, if negative notify the user saying "Negative number is not allowed. Enter the positive number". Like
    vb Code:
    1. Option Explicit
    2.  
    3. Sub main()
    4.     Dim I As Integer
    5.     I = InputBox("Enter the number:")
    6.     If I < 0 Then
    7.         Call MsgBox("The number you entered is negative. Please enter the positive numbers.", vbExclamation, App.Title)
    8.     Else
    9.         '... Your Calculations
    10.     End If
    11. End Sub
    Last edited by cssriraman; Apr 8th, 2007 at 10:54 PM.
    CS

  3. #3
    Member
    Join Date
    Nov 2006
    Posts
    40

    Re: "Negative" Probelm...

    cssriraman is correct!

    to avoid errors,,avoid values that made the errors!! FILTER THEM!!

  4. #4
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: "Negative" Probelm...

    To further avoid errors, be sure the value entered is actually a number. This may look slightly more complicated, but it really isn't.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.    If Not IsNumeric(Text1.Text) Then 'makes sure the entered number is a number
    6.        MsgBox "Please enter a numeric value."
    7.    Else 'if it is, continue
    8.       If CInt(Text1.Text) < 0 Then 'makes sure the entered number is non-negative
    9.          MsgBox "Please enter a positive number."
    10.       Else 'if it's a valid number, continue
    11.          'your calculations here
    12.       End If
    13.    End If
    14.  
    15. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    115

    Re: "Negative" Probelm...

    Oh thanks a lot guys, i remember doing this before. Must of got lost in the back of my head :S

    Thanks

  6. #6
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: "Negative" Probelm...

    No problem.

    Oh, and welcome to the forums!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    115

    Re: "Negative" Probelm...

    Thanks

  8. #8
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    Re: "Negative" Probelm...

    Couldn't the Abs() function be used in the calculation?

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: "Negative" Probelm...

    Quote Originally Posted by sneakers
    Couldn't the Abs() function be used in the calculation?
    Yes, but there is no such thing as a negative perfect square.

  10. #10
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    Re: "Negative" Probelm...

    I have been thinking about that since I posted that reply. A negative number requires a positive multiplied by a negative and a square number is a number multiplied by itself. I wasn't sure of the "laws of math" concerning this but logic alone says you can't have a true negative square number with a square root etc.

    However, the Abs() function could be used in a lost focus event to be sure the numbers are positive. This is just an easy way to filter out the negatives and software dealing with square roots has no need for a hyphen.

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