this program crashes and i don't know how to prevent it.

New to FreeBASIC? Post your questions here.
joshwiker14
Posts: 62
Joined: Nov 17, 2010 1:17
Contact:

this program crashes and i don't know how to prevent it.

Postby joshwiker14 » Dec 07, 2010 23:06

this program crashes and i know its because of a segmentation error but i dont know how to handle it, help please.

Code: Select all

'include code in commandbox.bi.
#include once "commandbox.bi"
'include code in null.bi.
#include "NULL.bi"
'dim variables.
Dim Shared As ulongint a,b
Dim Shared As String again
'declare subrotines.

Declare Sub prime (Byref a As ulongint, Byref b As ulongint)
'define subrotines.

Sub prime (Byref a As ulongint, Byref b As ulongint)
    While ((a / b) - Fix(a / b)) = 0
        Print b
        a = a / b
    Wend
    If b = 2 Then
        b = 3
    Else
        b += 2
    End If
    If b <= a Then :prime a,b
End If
End Sub

'set the screen resolution.

Screenres 640, 300, 32

'start program actions.
start:
Input " Number to factor?",a
start2:
if a > 18446744073709551615 then error 2
if err =2 then print " Number too large." :goto start
b = 2
'if a equals something (other than a number).
If a = null (a) Then
    Print " Please use numbers only!!!"
    Goto start
End If
prime a,b
Print " Done!"
Print ""
'end program

quit:
Input " Start again?", again
'if the user types a number into the prompt, find the factors.
If Val(again) <> 0 Then
    a = Val(again)
    Goto start2
Elseif Lcase(again) = "n" Then
        End 0
Else
    Print " What??"
    Print
    Goto quit
Endif

commandbox.bi

Code: Select all

#include once "fbgfx.bi"
#define _common_colors_
' add more colors
'the colors that can be used
Enum colors
    color_black = Rgb(0,0,0)
    color_blue = Rgb(0,0,255)
    color_cyan = Rgb(0,255,255)
    color_lightgreen = Rgb(0,170,50)
    color_darkgreen = Rgb(0,130,50)
    color_green = Rgb(0,255,0)
    color_yellow = Rgb(255,255,0)
    color_gold = Rgb(217,220,0)
    color_orange = Rgb(255,132,0)
    color_red = Rgb(255,0,0)
    color_lightgray = Rgb(200,200,200)
    color_gray = Rgb(150,150,150)
    color_darkgray = Rgb(100,100,100)
    color_purple = Rgb(100,0,100)
    color_velvet = Rgb(150,0,150)
    color_white = Rgb(255,255,255)
End Enum

null.bi

Code: Select all

'NULL-the value of nothing.
'in main program, use like this:
'if variable <> null(variable) then ....
Declare Function null Overload (Byref value As Ulongint) As Integer
Declare Function null Overload (Byref value As String) As Integer
Declare Function null (Byref value As Double) As Integer

Function null (Byref value As Ulongint) As Integer
    If value = 0 Then
        Return 0
    Else
        Return 1
    Endif
End Function
Function null (Byref value As String) As Integer
    If value <> "" Then
        Return 1
    Else
        Return 0
    Endif
End Function
Function null (Byref value As Double) As Integer
    If value = 0 Then
        Return 0
    Else
        Return 1
    Endif
End Function

also, how can i get this program to close with the 'x' button even while its waiting for input?
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Dec 07, 2010 23:18

this program crashes and i know its because of a segmentation error

Under what inputs or how often does it crash?
It runs OK for me under;
0.21.0 What version is your FB?
FBIde What is your IDE?
Win XP What is your version OS?
joshwiker14
Posts: 62
Joined: Nov 17, 2010 1:17
Contact:

Postby joshwiker14 » Dec 07, 2010 23:35

it crashes when the input is greater than the type can carry.(ie.9387543857394857394857)
compiler version .2
FBIde
window xp pro ver 2002 sp3
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Dec 07, 2010 23:57

Prime a, b is recursive but that should not be a problem since a maximum of 64 factors is possible with Ulongint.

A number cannot be bigger than the input data type. It would overflow. Your test is redundant so long as you use the input function.
Maybe write an input routine that watches the keys rather than does it for you. That way you can quit when you want.

You only need to search for divisibility of N by possible primes from the last divisor found to the Sqr(N).

There are some 8 digit even numbers that always terminate the program without any output, eg 62681058.
Last edited by Richard on Dec 08, 2010 0:02, edited 1 time in total.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Dec 08, 2010 0:01

I haven't looked at the code, but if the function is recursing more than 32 levels deep it will crash. I think it should warn you first though. I dunno.
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Postby j_milton » Dec 08, 2010 0:01

joshwiker14 wrote:it crashes when the input is greater than the type can carry.(ie.9387543857394857394857)
compiler version .2
FBIde
window xp pro ver 2002 sp3


this line in your program:
if a > 18446744073709551615 then error 2
tells the program to throw an error and end if the value entered by the user is greater than 18446744073709551615, which 9387543857394857394857 is, so it's doing what it has been told to do. Here is a simple example:

Code: Select all

Dim As Integer x
Input " Number to check?",x
If x >= 10 Then Error 2
Print "That number is less than 10 otherwise the program would have aborted by now"
Sleep
 

more info on error function is here:
http://www.freebasic.net/wiki/wikka.php ... KeyPgError
joshwiker14
Posts: 62
Joined: Nov 17, 2010 1:17
Contact:

Postby joshwiker14 » Dec 08, 2010 0:25

I attempted to handle the error but when i tried it the program closed itself when it's supposed to print "number too large" and prompt for another number. it closes with the exit code: -1073741819 which is tipically the exit code when the program was forced to close.
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Postby j_milton » Dec 08, 2010 0:28

joshwiker14 wrote:I attempted to handle the error but when i tried it the program closed itself when it's supposed to print "number too large" and prompt for another number.

try it like this

Code: Select all

On Error GoTo etrap
GoTo mainline

etrap:
Print "Number must be less than 10, press any key to continue"
sleep
GoTo mainline

mainline:
Dim As Integer x
Input "Number to check?",x
If x >= 10 Then Error 2
Print "That number is less than 10 otherwise the program would have aborted by now"
Sleep
 
joshwiker14
Posts: 62
Joined: Nov 17, 2010 1:17
Contact:

Postby joshwiker14 » Dec 08, 2010 0:45

j_milton wrote:
try it like this

Code: Select all

 
On Error GoTo etrap
GoTo mainline

etrap:
Print "Number must be less than 10, press any key to continue"
sleep
GoTo mainline

mainline:
Dim As Integer x
Input "Number to check?",x
If x >= 10 Then Error 2
Print "That number is less than 10 otherwise the program would have aborted by now"
Sleep


what about handling right after the error?

Code: Select all

'include code in commandbox.bi.
#include once "commandbox.bi"
'include code in null.bi.
#include "NULL.bi"
'dim variables.
Dim Shared As ulongint a,b
Dim Shared As String again
'declare subrotines.

Declare Sub prime (Byref a As ulongint, Byref b As ulongint)
'define subrotines.

Sub prime (Byref a As ulongint, Byref b As ulongint)
    While ((a / b) - Fix(a / b)) = 0
        Print b
        a = a / b
    Wend
    If b = 2 Then
        b = 3
    Else
        b += 2
    End If
    If b <= a Then :prime a,b
End If
End Sub
'set the screen resolution.
Screenres 640, 300, 32
'start program actions.
on error goto bignum
start:
Input " Number to factor?",a
start2:
if a > 18446744073709551615 then error 2
b = 2
'if a equals something (other than a number).
If a = null (a) Then
    Print " Please use numbers only!!!"
    Goto start
End If
prime a,b
Print " Done!"
Print ""
bignum:
if err =2 then print " Number too large." :goto start
'end program
quit:
Input " Start again?", again
'if the user types a number into the prompt, find the factors.
If Val(again) <> 0 Then
    a = Val(again)
    Goto start2
Elseif Lcase(again) = "n" Then
        End 0
Else
    Print " What??"
    Print
    Goto quit
Endif

as you can see i tried it anyway.
didn't make a difference.
Galeon
Posts: 563
Joined: Apr 08, 2009 5:30
Location: Philippines
Contact:

Postby Galeon » Dec 08, 2010 15:19

joshwiker14 wrote:j_milton wrote:
try it like this

Code: Select all

 
On Error GoTo etrap
GoTo mainline

etrap:
Print "Number must be less than 10, press any key to continue"
sleep
GoTo mainline

mainline:
Dim As Integer x
Input "Number to check?",x
If x >= 10 Then Error 2
Print "That number is less than 10 otherwise the program would have aborted by now"
Sleep


what about handling right after the error?

Code: Select all

'include code in commandbox.bi.
#include once "commandbox.bi"
'include code in null.bi.
#include "NULL.bi"
'dim variables.
Dim Shared As ulongint a,b
Dim Shared As String again
'declare subrotines.

Declare Sub prime (Byref a As ulongint, Byref b As ulongint)
'define subrotines.

Sub prime (Byref a As ulongint, Byref b As ulongint)
    While ((a / b) - Fix(a / b)) = 0
        Print b
        a = a / b
    Wend
    If b = 2 Then
        b = 3
    Else
        b += 2
    End If
    If b <= a Then :prime a,b
End If
End Sub
'set the screen resolution.
Screenres 640, 300, 32
'start program actions.
on error goto bignum
start:
Input " Number to factor?",a
start2:
if a > 18446744073709551615 then error 2
b = 2
'if a equals something (other than a number).
If a = null (a) Then
    Print " Please use numbers only!!!"
    Goto start
End If
prime a,b
Print " Done!"
Print ""
bignum:
if err =2 then print " Number too large." :goto start
'end program
quit:
Input " Start again?", again
'if the user types a number into the prompt, find the factors.
If Val(again) <> 0 Then
    a = Val(again)
    Goto start2
Elseif Lcase(again) = "n" Then
        End 0
Else
    Print " What??"
    Print
    Goto quit
Endif

as you can see i tried it anyway.
didn't make a difference.

Its because execution of programs do not start with the functions/subs, they start with first code that are not functions or subs, so they are the same when compiled, only the position in the source file is the difference. Goto will go anywhere, except on the top of the source file (it will executed first than other things).
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Postby Theunis Jansen » Dec 12, 2010 14:09

To exit an ERROR trap GOTO Start is incorrect. You must use RESUME Start.

However this is not relevant to the main problem so I am deleting my snippet which dealt with "ON ERROR GOTO"
Last edited by Theunis Jansen on Dec 13, 2010 8:21, edited 1 time in total.
fxm
Posts: 9260
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Dec 12, 2010 19:37

if a > 18446744073709551615 then error 2
if err =2 then print " Number too large." :goto start
is useless, because "input" limits all entered number to +18 446 744 073 709 551 615, without output any error.

To convert "again" (string) to "a" (ulongint), use :
a = Valulng(again)

Don't use recursion in procedure "prime", because depending on entered number, the stack size is not enough, and it is not advisable to increase it (that increases the fixed amount of memory needed to load and start your program, even if most of it is not used later, and can result in performance degrade, or even refusing your program to load at all).
Example : if the entered number "a" is a very large prime number, that induces about a/2 recursions!

Proposed program correcting

Code: Select all

'include code in commandbox.bi.
#include once "commandbox.bi"
'include code in null.bi.
#include "NULL.bi"
'dim variables.
Dim Shared As ulongint a,b
Dim Shared As String again
'declare subrotines.

Declare Sub prime (Byref a As ulongint, Byref b As ulongint)
'define subrotines.

Sub prime (Byref a As ulongint, Byref b As ulongint)
  While b <= a
    While ((a / b) - Fix(a / b)) = 0
        Print b
        a = a / b
    Wend
    If b = 2 Then
        b = 3
    Else
        b += 2
    End If
  Wend
'    If b <= a Then :prime a,b
'End If
End Sub

'set the screen resolution.

Screenres 640, 300, 32

'start program actions.
start:
Input " Number to factor?",a
start2:
'if a > 18446744073709551615 then error 2
'if err =2 then print " Number too large." :goto start
b = 2
'if a equals something (other than a number).
If a = null (a) Then
    Print " Please use numbers only!!!"
    Goto start
End If
prime a,b
Print " Done!"
Print ""
'end program

quit:
Input " Start again?", again
'if the user types a number into the prompt, find the factors.
If Val(again) <> 0 Then
    a = Valulng(again)
    Goto start2
Elseif Lcase(again) = "n" Then
        End 0
Else
    Print " What??"
    Print
    Goto quit
Endif
 

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest