QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: badger on September 29, 2020, 12:00:38 pm

Title: memory leak and question
Post by: badger on September 29, 2020, 12:00:38 pm
Hello

First the question. When a sub is call when the end sub or exit sub is encountered does it return to the next statement after the call

memory leak i am going to post my code here. I use rem statement to an excess do to my eyes so the code will not run together so please dont pound on me about the clutter ok just look at the prompt area and tell me if i left any memory leaks

thanks in advance
Badger
if this is not allowed sorry please delete and let me know how to post code


REM ***************************************************************************
REM **** Program Bridal Store *************************************************
REM ***************************************************************************
REM ***************************************************************************
REM **** Author Philip King  **************************************************
REM ***************************************************************************
REM ***************************************************************************
REM ***************************************************************************
REM **** Includes go here *****************************************************
REM ***************************************************************************

REM ***************************************************************************
REM ***************************************************************************
REM **** Sreen statements here ************************************************
REM ***************************************************************************

$COLOR:32

REM ***************************************************************************

REM ***************************************************************************
REM **** Listing of subs and funtions *****************************************
REM ***************************************************************************

REM ***************************************************************************

REM ***************************************************************************
REM **** arrays decleared here ************************************************
REM ***************************************************************************
imenu = 4
DIM SHARED smenu(imenu) AS STRING

REM ***************************************************************************

REM ***************************************************************************
REM **** arrays set to data here **********************************************
REM ***************************************************************************

smenu(1) = "1) Edit Customer Info": smenu(2) = "3) Edit Inventory Data "
smenu(3) = "2) Edit Sales ": smenu(4) = "4) System Maintenance "

REM ***************************************************************************
REM **** variables declares go here *******************************************
REM ***************************************************************************
DIM SHARED sr AS STRING
CONST null = ""
REM ***************************************************************************

REM ***************************************************************************
REM **** customer info record type ********************************************
REM ***************************************************************************
TYPE customer
        id AS LONG
        sfirst_name AS STRING * 25
        slast_name AS STRING * 25
        saddress AS STRING * 30
        scity AS STRING * 25
        sstate AS STRING * 2
        szip AS STRING * 5
        sphone AS STRING * 10
END TYPE
REM ***************************************************************************

REM ***************************************************************************
REM **** inventory record type ************************************************
REM ***************************************************************************
TYPE inventory
        ienventory AS LONG
        sdesc AS STRING * 250
        sdate_bought AS STRING * 8
        sdate_sold AS STRING * 8
        fcost AS _FLOAT
        fretail AS _FLOAT
        ssize AS STRING * 2
        ssold AS STRING * 1
END TYPE
REM ***************************************************************************

REM ***************************************************************************
REM **** point of sale type ***************************************************
REM ***************************************************************************
TYPE spos
        lposid AS LONG
END TYPE
REM ***************************************************************************
DIM SHARED custinforec AS customer
custinforeclen = LEN(custinforec)

CLS


REM ***************************************************************************
REM **** defines for quick use data types *************************************
REM ***************************************************************************
_DEFINE I AS INTEGER : _DEFINE S AS STRING : _DEFINE D AS DOUBLE :
_DEFINE F AS _FLOAT : _DEFINE L AS LONG
REM ***************************************************************************


REM ***************************************************************************
REM **** main modual **********************************************************
REM ***************************************************************************
SCREEN _NEWIMAGE(640, 400, 32)
COLOR LightCyan, MidnightBlue
iflag2 = 0
iflag3 = 1
CLS
'SCREEN 12
_FULLSCREEN
IF se = "X" THEN END
GOSUB menuloop:
IF saction = "X" THEN END
'if se ="1" then call that prompt
'LINE INPUT siy: END
REM ***************************************************************************
REM **** main menu loop *******************************************************
REM ***************************************************************************

menuloop:
CALL systemheader
CALL printmain
CALL mainmenuchoice
IF sr = "" OR sr = CHR$(13) THEN GOTO menuloop
IF sr = "X" THEN END
'IF sr = "1" THEN CALL custinfo: 'GOSUB custinfo1
IF sr = "1" THEN
        CALL promptid
        CALL promptfname
        CALL promptlname
        CALL promptaddress
        CALL promptcity
        CALL promptsstate
        CALL promptszip
        CALL promptphone
        RETURN
END IF
REM ***************************************************************************




REM ***************************************************************************
REM **** customer input modual ************************************************
REM ***************************************************************************

's0:
SUB custinput
        CALL systemheader
        LOCATE 10, 1: PRINT "Customer ID           ";: PRINT custinforec.id
        LOCATE 11, 1: PRINT "Customer First Name   ";: PRINT custinforec.sfirst_name
        LOCATE 12, 1: PRINT "customer Last Name    ";: PRINT custinforec.slast_name
        LOCATE 13, 1: PRINT "Customer Address      ";: PRINT custinforec.saddress
        LOCATE 14, 1: PRINT "Customer City         ";: PRINT custinforec.scity
        LOCATE 15, 1: PRINT "customer State        ";: PRINT custinforec.sstate
        LOCATE 16, 1: PRINT "customer Zip          ";: PRINT custinforec.szip
        LOCATE 17, 1: PRINT "Customer Phone Number ";
        PRINT LEFT$(custinforec.sphone, 3) + "/"; MID$(custinforec.sphone, 4, 3) + "/"; RIGHT$(custinforec.sphone, 4)
        'RETURN
END SUB
REM ********************************************************************
's1:
SUB promptid
        CALL custinput
        saction = "Enter Customer ID X to Exit "
        ib = 9
        ilow = 1
        ihigh = 999999999
        iflag1 = 1
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF
        IF sr = "-" THEN
                EXIT SUB
                RETURN
        END IF
        IF sr = "X" THEN
                iflag1 = 0
                EXIT SUB
                RETURN
        END IF
        custinforec.id = VAL(sr): CALL custinput
END SUB
REM ********************************************************************
's2:
SUB promptfname
        CALL custinput
        saction = "Enter First Name X to Exit - To Backup "
        ib = 25
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptid
        END IF
        IF sr = "X" THEN
                EXIT SUB
        END IF

        custinforec.sfirst_name = sr: CALL custinput
END SUB
REM ***************************************************************************
's3:
SUB promptlname
        CALL custinput
        saction = "Enter Last Name X to Exit To Back up "
        ib = 25
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        '        IF sr = "-" THEN
        '                EXIT SUB
        '                CALL promptfname
        '       END IF
        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.slast_name = sr
        CALL custinput
END SUB
REM ***************************************************************************
's4:
SUB promptaddress
        CALL custinput
        saction = "Enter Address X to Exit - To Back up "
        ib = 30
        ilow = 99
        ihigh = 99
        iflag1 = 0

        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptlname
        END IF



        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.saddress = sr
        CALL custinput
END SUB
REM ***************************************************************************
's5:
SUB promptcity
        CALL custinput
        saction = "Enter City Name X to Exit - To Back up "
        ib = 25
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptaddress
        END IF
        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.scity = sr
        CALL custinput
END SUB
REM ***************************************************************************
's6:
SUB promptsstate
        CALL custinput
        saction = "Enter State X to Exit - To Back up "
        ib = 2
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptcity
        END IF
        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.sstate = sr
        CALL custinput
END SUB

REM ***************************************************************************

's7:
SUB promptszip
        CALL custinput
        saction = "Enter Zip Code Name X to Exit - To Back up "
        ib = 5
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptsstate
        END IF
        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.szip = sr
        CALL custinput
END SUB
REM ***************************************************************************
's8:
SUB promptphone
        CALL custinput
        saction = "Enter Phone Number X to Exit - To Back up "
        ib = 30
        ilow = 99
        ihigh = 99
        iflag1 = 0
        IF iflag2 = 1 THEN
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        ELSE
                CALL clearprompt
                CALL salphainput(saction, ib, ilow, ihigh, iflag1)
        END IF

        IF sr = "-" THEN
                EXIT SUB
                CALL promptszip
        END IF
        IF sr = "X" THEN
                EXIT SUB
                RETURN
        END IF
        custinforec.sphone = sr
        CALL custinput
        LINE INPUT sr
END SUB

REM ***************************************************************************


REM ***************************************************************************
REM **** set custinforrec variables to zero or null ***************************
REM ***************************************************************************
SUB custinforec_null
        custinforec.id = 0
        custinforec.sfirst_name = null
        custinforec.slast_name = null
        custinforec.saddress = null
        custinforec.scity = null
        custinforec.sstate = null
        custinforec.szip = null
        custinforec.sphone = null
END SUB
REM ***************************************************************************

REM ***************************************************************************
REM **** Sytem Header *********************************************************
REM ***************************************************************************
SUB systemheader
        COLOR White, Blue
        CLS
        LOCATE 1, 1: PRINT CHR$(201): LOCATE 1, 80: PRINT CHR$(187)
        LOCATE 1, 2: PRINT STRING$(78, 205):
        FOR ix = 2 TO 7
                LOCATE ix, 1: PRINT CHR$(186)
                LOCATE ix, 80: PRINT CHR$(186)
        NEXT ix
        LOCATE 7, 2: PRINT STRING$(78, 205)
        LOCATE 7, 1: PRINT CHR$(200): LOCATE 7, 80: PRINT CHR$(188)
        LOCATE 4, 30: PRINT "Main System Menu"
        LOCATE 4, 63: PRINT "Date ": LOCATE 4, 68: PRINT DATE$
        LOCATE 4, 4: PRINT "Virsion 1.0"
END SUB



REM ***************************************************************************
REM **** printes in system erros **********************************************
REM ***************************************************************************
SUB sprintserrors (se)
        CLS
        SCREEN 0
        IF se = "" THEN se = "INVALID RESPONSE"
        itemp = LEN(se)
        itemp1 = 40 - (itemp / 2)
        BEEP
        COLOR 16, 7
        'COLOR Yellow, Blue
        LOCATE 25, itemp1: _BLINK ON: PRINT se;
        Delay 1.5
        _BLINK OFF
        $COLOR:0
        iflag2 = 1
END SUB
REM ***************************************************************************

REM *****************************************************************************
REM **** numeric input sub ******************************************************
REM *****************************************************************************
SUB snumeric (sr, ilow, ihigh)

        ftemp = VAL(sr)
        IF ftemp < ilow OR ftemp > ihigh THEN
                se = "INPUT OUT OF BOUNDS"
                CALL sprintserrors(se)
        END IF

END SUB
REM ***************************************************************************

REM ***************************************************************************
REM **** alpha input sub ******************************************************
REM ***************************************************************************
SUB salphainput (saction, ib, ilow, ihigh, iflag1)

        iflag2 = 0
        ilength1 = LEN(sr)
        ilength2 = LEN(saction) + 2

        LOCATE 23, 1: PRINT saction; '               prints action menu
        LOCATE 23, ilength2 '                        set prompt one space after action menu print
        COLOR Yellow, Black: PRINT SPC(ib); '                changes color and prints ib spaces to screen for input lenght
        LOCATE 23, ilength2 '                        sets curser at the begining of input space from previouse line
        LINE INPUT sr: sr = UCASE$(sr) '             inputs data from user changes it to upper case

        IF LEN(sr) > ib THEN
                se = "ENVALID INPUT LENGTH"
                CALL sprintserrors(se)
                sr = ""
                iflag2 = 1
        END IF
        IF iflag1 = 1 AND sr = "X" THEN iflag1 = 0
        IF iflag1 = 1 AND VAL(sr) < ilow AND VAL(sr) > ihigh THEN CALL snumeric(sr, ilow, ihigh)

        COLOR White, Blue
END SUB


REM ***************************************************************************
REM **** main menu to the screen **********************************************
REM ***************************************************************************
SUB printmain
        COLOR White, Blue
        FOR ix = 11 TO imenu
                READ smenu(ix): LOCATE ix, 20: PRINT smenu(ix)
        NEXT ix

        LOCATE 11, 15: PRINT smenu(1)
        LOCATE 11, 40: PRINT smenu(2)
        LOCATE 13, 15: PRINT smenu(3)
        LOCATE 13, 40: PRINT smenu(4)

END SUB
REM ***************************************************************************

REM ***************************************************************************
REM **** array data goes here *************************************************
REM ***************************************************************************

SUB Delay (dlay!)
        start! = TIMER
        DO WHILE start! + dlay! >= TIMER
                IF start! > TIMER THEN start! = start! - 86400
        LOOP
END SUB

REM ***************************************************************************

REM ***************************************************************************
REM **** menu prompts start here **********************************************
REM ***************************************************************************
SUB mainmenuchoice
        ib = 1
        ihigh = 99
        ilow = 1
        saction = "Choose Menu Option or X to Quit "
        CALL salphainput(saction, ib, ilow, ihigh, iflag1)
END SUB
REM ***************************************************************************

REM ***************************************************************************
REM **** clean up the prompt area *********************************************
REM ***************************************************************************

SUB clearprompt
        LOCATE 23, 1: PRINT STRING$(79, 32)
END SUB

REM ***************************************************************************

Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 12:20:30 pm
Quote
First the question. When a sub is call when the end sub or exit sub is encountered does it return to the next statement after the call

Yes!

For posting code here at forum,
paste copy of code into editor, select it, and then use Code Tag button looks like QB64 icon:
  [ This attachment cannot be displayed inline in 'Print Page' view ]  


Title: Re: memory leak and question
Post by: badger on September 29, 2020, 12:27:27 pm
Hello

will do

Badger
like this

Code: QB64: [Select]
  1. REM ***************************************************************************
  2. REM **** Program Bridal Store *************************************************
  3. REM ***************************************************************************
  4. REM ***************************************************************************
  5. REM **** Author Philip King  **************************************************
  6. REM ***************************************************************************
  7. REM ***************************************************************************
  8. REM ***************************************************************************
  9. REM **** Includes go here *****************************************************
  10. REM ***************************************************************************
  11.  
  12. REM ***************************************************************************
  13. REM ***************************************************************************
  14. REM **** Sreen statements here ************************************************
  15. REM ***************************************************************************
  16.  
  17.  
  18. REM ***************************************************************************
  19.  
  20. REM ***************************************************************************
  21. REM **** Listing of subs and funtions *****************************************
  22. REM ***************************************************************************
  23.  
  24. REM ***************************************************************************
  25.  
  26. REM ***************************************************************************
  27. REM **** arrays decleared here ************************************************
  28. REM ***************************************************************************
  29. imenu = 4
  30. DIM SHARED smenu(imenu) AS STRING
  31.  
  32. REM ***************************************************************************
  33.  
  34. REM ***************************************************************************
  35. REM **** arrays set to data here **********************************************
  36. REM ***************************************************************************
  37.  
  38. smenu(1) = "1) Edit Customer Info": smenu(2) = "3) Edit Inventory Data "
  39. smenu(3) = "2) Edit Sales ": smenu(4) = "4) System Maintenance "
  40.  
  41. REM ***************************************************************************
  42. REM **** variables declares go here *******************************************
  43. REM ***************************************************************************
  44. CONST null = ""
  45. REM ***************************************************************************
  46.  
  47. REM ***************************************************************************
  48. REM **** customer info record type ********************************************
  49. REM ***************************************************************************
  50. TYPE customer
  51.         id AS LONG
  52.         sfirst_name AS STRING * 25
  53.         slast_name AS STRING * 25
  54.         saddress AS STRING * 30
  55.         scity AS STRING * 25
  56.         sstate AS STRING * 2
  57.         szip AS STRING * 5
  58.         sphone AS STRING * 10
  59. REM ***************************************************************************
  60.  
  61. REM ***************************************************************************
  62. REM **** inventory record type ************************************************
  63. REM ***************************************************************************
  64. TYPE inventory
  65.         ienventory AS LONG
  66.         sdesc AS STRING * 250
  67.         sdate_bought AS STRING * 8
  68.         sdate_sold AS STRING * 8
  69.         fcost AS _FLOAT
  70.         fretail AS _FLOAT
  71.         ssize AS STRING * 2
  72.         ssold AS STRING * 1
  73. REM ***************************************************************************
  74.  
  75. REM ***************************************************************************
  76. REM **** point of sale type ***************************************************
  77. REM ***************************************************************************
  78. TYPE spos
  79.         lposid AS LONG
  80. REM ***************************************************************************
  81. DIM SHARED custinforec AS customer
  82. lcustinforeclen = LEN(custinforec)
  83.  
  84.  
  85.  
  86. REM ***************************************************************************
  87. REM **** defines for quick use data types *************************************
  88. REM ***************************************************************************
  89. REM ***************************************************************************
  90.  
  91.  
  92. REM ***************************************************************************
  93. REM **** main modual **********************************************************
  94. REM ***************************************************************************
  95. SCREEN _NEWIMAGE(640, 400, 32)
  96. COLOR LightCyan, MidnightBlue
  97. iflag2 = 0
  98. iflag3 = 1
  99. IF se = "X" THEN END
  100. GOSUB menuloop:
  101. REM ***************************************************************************
  102. REM **** main menu loop *******************************************************
  103. REM ***************************************************************************
  104.  
  105. menuloop:
  106. CALL systemheader
  107. CALL printmain
  108. CALL mainmenuchoice
  109. IF sr = "" OR sr = CHR$(13) THEN GOTO menuloop
  110. IF sr = "X" THEN END
  111. 'IF sr = "1" THEN CALL custinfo: 'GOSUB custinfo1
  112.  
  113. IF sr = "1" THEN
  114.         CALL promptid
  115.         IF sr = "X" THEN RETURN
  116.         CALL promptfname
  117.         IF sr = "X" THEN RETURN
  118.         CALL promptlname
  119.         IF sr = "X" THEN RETURN
  120.         CALL promptaddress
  121.         IF sr = "X" THEN RETURN
  122.         CALL promptcity
  123.         IF sr = "X" THEN RETURN
  124.         CALL promptsstate
  125.         IF sr = "X" THEN RETURN
  126.         CALL promptszip
  127.         IF sr = "X" THEN RETURN
  128.         CALL promptphone
  129.         IF sr = "X" THEN RETURN
  130.         RETURN
  131. REM ***************************************************************************
  132.  
  133. REM ***************************************************************************
  134. REM **** customer input modual ************************************************
  135. REM ***************************************************************************
  136.  
  137. 's0:
  138. SUB custinput
  139.         CALL systemheader
  140.         LOCATE 10, 1: PRINT "Customer ID           ";: PRINT custinforec.id
  141.         LOCATE 11, 1: PRINT "Customer First Name   ";: PRINT custinforec.sfirst_name
  142.         LOCATE 12, 1: PRINT "customer Last Name    ";: PRINT custinforec.slast_name
  143.         LOCATE 13, 1: PRINT "Customer Address      ";: PRINT custinforec.saddress
  144.         LOCATE 14, 1: PRINT "Customer City         ";: PRINT custinforec.scity
  145.         LOCATE 15, 1: PRINT "customer State        ";: PRINT custinforec.sstate
  146.         LOCATE 16, 1: PRINT "customer Zip          ";: PRINT custinforec.szip
  147.         LOCATE 17, 1: PRINT "Customer Phone Number ";
  148.         PRINT LEFT$(custinforec.sphone, 3) + "/"; MID$(custinforec.sphone, 4, 3) + "/"; RIGHT$(custinforec.sphone, 4)
  149.         'RETURN
  150.  
  151. REM ********************************************************************
  152.  
  153. 's1:
  154. SUB promptid
  155.         CALL custinput
  156.         saction = "Enter Customer ID X to Exit "
  157.         ib = 9
  158.         ilow = 1
  159.         ihigh = 999999999
  160.         iflag1 = 1
  161.         IF iflag2 = 1 THEN
  162.                 CALL clearprompt
  163.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  164.         ELSE
  165.                 CALL clearprompt
  166.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  167.         END IF
  168.         IF sr = "X" THEN
  169.                 iflag1 = 0
  170.                 EXIT SUB
  171.                 ' RETURN
  172.         END IF
  173.         custinforec.id = VAL(sr): CALL custinput
  174.  
  175. REM ********************************************************************
  176.  
  177. 's2:
  178. SUB promptfname
  179.         CALL custinput
  180.         saction = "Enter First Name X to Exit - To Backup "
  181.         ib = 25
  182.         ilow = 99
  183.         ihigh = 99
  184.         iflag1 = 0
  185.         IF iflag2 = 1 THEN
  186.                 CALL clearprompt
  187.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  188.         ELSE
  189.                 CALL clearprompt
  190.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  191.         END IF
  192.  
  193.         IF sr = "-" THEN
  194.                 EXIT SUB
  195.                 CALL promptid
  196.         END IF
  197.         IF sr = "X" THEN
  198.                 EXIT SUB
  199.         END IF
  200.  
  201.         custinforec.sfirst_name = sr: CALL custinput
  202.  
  203. REM ***************************************************************************
  204.  
  205. 's3:
  206.  
  207. SUB promptlname
  208.         CALL custinput
  209.         saction = "Enter Last Name X to Exit - To Back up "
  210.         ib = 25
  211.         ilow = 99
  212.         ihigh = 99
  213.         iflag1 = 0
  214.         IF iflag2 = 1 THEN
  215.                 CALL clearprompt
  216.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  217.         ELSE
  218.                 CALL clearprompt
  219.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  220.         END IF
  221.  
  222.         '        IF sr = "-" THEN
  223.         '                EXIT SUB
  224.         '                CALL promptfname
  225.         '       END IF
  226.         IF sr = "X" THEN
  227.                 EXIT SUB
  228.                 RETURN
  229.         END IF
  230.         custinforec.slast_name = sr
  231.         CALL custinput
  232. REM ***************************************************************************
  233.  
  234. 's4:
  235. SUB promptaddress
  236.         CALL custinput
  237.         saction = "Enter Address X to Exit - To Back up "
  238.         ib = 30
  239.         ilow = 99
  240.         ihigh = 99
  241.         iflag1 = 0
  242.  
  243.         IF iflag2 = 1 THEN
  244.                 CALL clearprompt
  245.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  246.         ELSE
  247.                 CALL clearprompt
  248.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  249.         END IF
  250.  
  251.         IF sr = "-" THEN
  252.                 EXIT SUB
  253.                 CALL promptlname
  254.         END IF
  255.  
  256.  
  257.  
  258.         IF sr = "X" THEN
  259.                 EXIT SUB
  260.                 RETURN
  261.         END IF
  262.         custinforec.saddress = sr
  263.         CALL custinput
  264. REM ***************************************************************************
  265.  
  266. 's5:
  267. SUB promptcity
  268.         CALL custinput
  269.         saction = "Enter City Name X to Exit - To Back up "
  270.         ib = 25
  271.         ilow = 99
  272.         ihigh = 99
  273.         iflag1 = 0
  274.         IF iflag2 = 1 THEN
  275.                 CALL clearprompt
  276.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  277.         ELSE
  278.                 CALL clearprompt
  279.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  280.         END IF
  281.  
  282.         IF sr = "-" THEN
  283.                 EXIT SUB
  284.                 CALL promptaddress
  285.         END IF
  286.         IF sr = "X" THEN
  287.                 EXIT SUB
  288.                 RETURN
  289.         END IF
  290.         custinforec.scity = sr
  291.         CALL custinput
  292. REM ***************************************************************************
  293. 's6:
  294. SUB promptsstate
  295.         CALL custinput
  296.         saction = "Enter State X to Exit - To Back up "
  297.         ib = 2
  298.         ilow = 99
  299.         ihigh = 99
  300.         iflag1 = 0
  301.         IF iflag2 = 1 THEN
  302.                 CALL clearprompt
  303.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  304.         ELSE
  305.                 CALL clearprompt
  306.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  307.         END IF
  308.  
  309.         IF sr = "-" THEN
  310.                 EXIT SUB
  311.                 CALL promptcity
  312.         END IF
  313.         IF sr = "X" THEN
  314.                 EXIT SUB
  315.                 RETURN
  316.         END IF
  317.         custinforec.sstate = sr
  318.         CALL custinput
  319.  
  320. REM ***************************************************************************
  321.  
  322. 's7:
  323. SUB promptszip
  324.         CALL custinput
  325.         saction = "Enter Zip Code Name X to Exit - To Back up "
  326.         ib = 5
  327.         ilow = 99
  328.         ihigh = 99
  329.         iflag1 = 0
  330.         IF iflag2 = 1 THEN
  331.                 CALL clearprompt
  332.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  333.         ELSE
  334.                 CALL clearprompt
  335.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  336.         END IF
  337.  
  338.         IF sr = "-" THEN
  339.                 EXIT SUB
  340.                 CALL promptsstate
  341.         END IF
  342.         IF sr = "X" THEN
  343.                 EXIT SUB
  344.                 RETURN
  345.         END IF
  346.         custinforec.szip = sr
  347.         CALL custinput
  348. REM ***************************************************************************
  349.  
  350. 's8:
  351. SUB promptphone
  352.         CALL custinput
  353.         saction = "Enter Phone Number X to Exit - To Back up "
  354.         ib = 30
  355.         ilow = 99
  356.         ihigh = 99
  357.         iflag1 = 0
  358.         IF iflag2 = 1 THEN
  359.                 CALL clearprompt
  360.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  361.         ELSE
  362.                 CALL clearprompt
  363.                 CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  364.         END IF
  365.  
  366.         IF sr = "-" THEN
  367.                 EXIT SUB
  368.                 CALL promptszip
  369.         END IF
  370.         IF sr = "X" THEN
  371.                 EXIT SUB
  372.                 RETURN
  373.         END IF
  374.         custinforec.sphone = sr
  375.         CALL custinput
  376.         LINE INPUT sr
  377.  
  378. REM ***************************************************************************
  379.  
  380.  
  381. REM ***************************************************************************
  382. REM **** set custinforrec variables to zero or null ***************************
  383. REM ***************************************************************************
  384. SUB custinforec_null
  385.         custinforec.id = 0
  386.         custinforec.sfirst_name = null
  387.         custinforec.slast_name = null
  388.         custinforec.saddress = null
  389.         custinforec.scity = null
  390.         custinforec.sstate = null
  391.         custinforec.szip = null
  392.         custinforec.sphone = null
  393. REM ***************************************************************************
  394.  
  395. REM ***************************************************************************
  396. REM **** Sytem Header *********************************************************
  397. REM ***************************************************************************
  398. SUB systemheader
  399.         COLOR White, Blue
  400.         CLS
  401.         LOCATE 1, 1: PRINT CHR$(201): LOCATE 1, 80: PRINT CHR$(187)
  402.         LOCATE 1, 2: PRINT STRING$(78, 205):
  403.         FOR ix = 2 TO 7
  404.                 LOCATE ix, 1: PRINT CHR$(186)
  405.                 LOCATE ix, 80: PRINT CHR$(186)
  406.         NEXT ix
  407.         LOCATE 7, 2: PRINT STRING$(78, 205)
  408.         LOCATE 7, 1: PRINT CHR$(200): LOCATE 7, 80: PRINT CHR$(188)
  409.         LOCATE 4, 30: PRINT "Main System Menu"
  410.         LOCATE 4, 63: PRINT "Date ": LOCATE 4, 68: PRINT DATE$
  411.         LOCATE 4, 4: PRINT "Virsion 1.0"
  412.  
  413.  
  414.  
  415. REM ***************************************************************************
  416. REM **** printes in system erros **********************************************
  417. REM ***************************************************************************
  418. SUB sprintserrors (se)
  419.         CLS
  420.         SCREEN 0
  421.         IF se = "" THEN se = "INVALID RESPONSE"
  422.         itemp = LEN(se)
  423.         itemp1 = 40 - (itemp / 2)
  424.         BEEP
  425.         COLOR 16, 7
  426.         'COLOR Yellow, Blue
  427.         LOCATE 25, itemp1: _BLINK ON: PRINT se;
  428.         Delay 1.5
  429.         _BLINK OFF
  430.         $COLOR:0
  431.         iflag2 = 1
  432. REM ***************************************************************************
  433.  
  434. REM *****************************************************************************
  435. REM **** numeric input sub ******************************************************
  436. REM *****************************************************************************
  437. SUB snumeric (sr, ilow, ihigh)
  438.  
  439.         ftemp = VAL(sr)
  440.         IF ftemp < ilow OR ftemp > ihigh THEN
  441.                 se = "INPUT OUT OF BOUNDS"
  442.                 CALL sprintserrors(se)
  443.         END IF
  444.  
  445. REM ***************************************************************************
  446.  
  447. REM ***************************************************************************
  448. REM **** alpha input sub ******************************************************
  449. REM ***************************************************************************
  450. SUB salphainput (saction, ib, ilow, ihigh, iflag1)
  451.  
  452.         iflag2 = 0
  453.         ilength1 = LEN(sr)
  454.         ilength2 = LEN(saction) + 2
  455.  
  456.         LOCATE 23, 1: PRINT saction; '               prints action menu
  457.         LOCATE 23, ilength2 '                        set prompt one space after action menu print
  458.         COLOR Yellow, Black: PRINT SPC(ib); '                changes color and prints ib spaces to screen for input lenght
  459.         LOCATE 23, ilength2 '                        sets curser at the begining of input space from previouse line
  460.         LINE INPUT sr: sr = UCASE$(sr) '             inputs data from user changes it to upper case
  461.  
  462.         IF LEN(sr) > ib THEN
  463.                 se = "ENVALID INPUT LENGTH"
  464.                 CALL sprintserrors(se)
  465.                 sr = ""
  466.                 iflag2 = 1
  467.         END IF
  468.         IF iflag1 = 1 AND sr = "X" THEN iflag1 = 0
  469.         IF iflag1 = 1 AND VAL(sr) < ilow AND VAL(sr) > ihigh THEN CALL snumeric(sr, ilow, ihigh)
  470.  
  471.         COLOR White, Blue
  472.  
  473.  
  474. REM ***************************************************************************
  475. REM **** main menu to the screen **********************************************
  476. REM ***************************************************************************
  477. SUB printmain
  478.         COLOR White, Blue
  479.         FOR ix = 11 TO imenu
  480.                 READ smenu(ix): LOCATE ix, 20: PRINT smenu(ix)
  481.         NEXT ix
  482.  
  483.         LOCATE 11, 15: PRINT smenu(1)
  484.         LOCATE 11, 40: PRINT smenu(2)
  485.         LOCATE 13, 15: PRINT smenu(3)
  486.         LOCATE 13, 40: PRINT smenu(4)
  487.  
  488. REM ***************************************************************************
  489.  
  490. REM ***************************************************************************
  491. REM **** array data goes here *************************************************
  492. REM ***************************************************************************
  493.  
  494. SUB Delay (dlay!)
  495.         start! = TIMER
  496.         DO WHILE start! + dlay! >= TIMER
  497.                 IF start! > TIMER THEN start! = start! - 86400
  498.         LOOP
  499.  
  500. REM ***************************************************************************
  501.  
  502. REM ***************************************************************************
  503. REM **** menu prompts start here **********************************************
  504. REM ***************************************************************************
  505. SUB mainmenuchoice
  506.         ib = 1
  507.         ihigh = 99
  508.         ilow = 1
  509.         saction = "Choose Menu Option or X to Quit "
  510.         CALL salphainput(saction, ib, ilow, ihigh, iflag1)
  511. REM ***************************************************************************
  512.  
  513. REM ***************************************************************************
  514. REM **** clean up the prompt area *********************************************
  515. REM ***************************************************************************
  516.  
  517. SUB clearprompt
  518.         LOCATE 23, 1: PRINT STRING$(79, 32)
  519.  
  520. REM ***************************************************************************
  521.  
  522.  
Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 12:32:23 pm
Yes! Much better!

Not seeing a loop here:
Code: QB64: [Select]
  1. menuloop:
  2. CALL systemheader
  3. CALL printmain
  4. CALL mainmenuchoice
  5. IF sr = "" OR sr = CHR$(13) THEN GOTO menuloop
  6. IF sr = "X" THEN END
  7. 'IF sr = "1" THEN CALL custinfo: 'GOSUB custinfo1
  8. IF sr = "1" THEN
  9.     CALL promptid
  10.     CALL promptfname
  11.     CALL promptlname
  12.     CALL promptaddress
  13.     CALL promptcity
  14.     CALL promptsstate
  15.     CALL promptszip
  16.     CALL promptphone
  17.     RETURN ' <<<<<<<<<<<<<<<<<<<<<<< this is trouble!
  18.  
  19. 's0:
  20. SUB custinput
  21.  

How are you coming back from SUB calls and looping around for menu again?
A simple
Code: QB64: [Select]
  1. GOTO menuloop
would do it, just before the first SUB.

BTW you don't have to CALL a sub, the sub name will do it.


Update: O hell now I see you put that dang mainLoop in a GOSUB, missed this
Code: QB64: [Select]
  1. IF se = "X" THEN END
  2. GOSUB menuloop:  ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< oh here it is
  3. REM ***************************************************************************
  4. REM **** main menu LOOP *******************************************************
  5. REM

Well pooh, who puts the mainloop in a GOSUB?
Title: Re: memory leak and question
Post by: MelliLewis on September 29, 2020, 12:39:58 pm
Hello...There are various wellsprings of memory spills. The utilization of strings is one. An instructional exercise on all memory spill issues would take what might be compared to a section in an Intro to DXL book.

String source memory holes can be diminished yet not disposed of by utilizing cushions. It is significant when utilizing supports to abstain from string link when adding string factors to the cushion. For instance if S1, S2 and S3 are string factors and buText is a support, don't do the accompanying to connect the strings.
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 12:43:17 pm
Hello

i will try that i have other things to do off the main menu but i can always go back to the main by goto as well

Badger
Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 12:44:59 pm
Code: QB64: [Select]
  1.     CALL promptid
  2.     CALL promptfname
  3.     CALL promptlname
  4.     CALL promptaddress
  5.     CALL promptcity
  6.     CALL promptsstate
  7.     CALL promptszip
  8.     CALL promptphone
  9.  

You have this code so fragmented and broken up, that it is way more complex than need be.

The above code can just be one or two screens of INPUT lines, one sub routine!
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 12:50:36 pm
Hello

I think i understand what you are trying to say. but i only use one input statement then set conditions on each prompt

Badger

this was the way i was taught. This is also the first time of using subs i can to this much easier with gosubs and gotos

Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 12:59:26 pm
Well I appreciate the fact you are trying to use SUBs for first time but you can write a Customer Data Entry Form with 1 SUB in about 50 lines.

You just want to get all this into a customer record:
Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     sfirst_name AS STRING * 25
  4.     slast_name AS STRING * 25
  5.     saddress AS STRING * 30
  6.     scity AS STRING * 25
  7.     sstate AS STRING * 2
  8.     szip AS STRING * 5
  9.     sphone AS STRING * 10
  10.  

Correct?
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 01:02:28 pm
Hello

yes that is correct then the next step is into a random file

Phil
Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 01:16:35 pm
Imagine how one SUB can take over screen and get all this info and return a Customer Record:

Code: QB64: [Select]
  1. SUB GetCustomerInfo(custRecord as Customer)
  2. DIM rec as Customer
  3.  
  4. PRINT " Customer Data Form:"
  5. INPUT "Enter customer first name "; rec.name_first
  6. INPUT "Enter customer last name "; rec.name_last
  7. ...
  8.  
  9. 'rec.id   ' probably assigned according to record number so leave black and fill in later
  10.  
  11. custRecord = rec ' Sub passes custRec back to main caller code

As time goes on you can refine this very crude input form with checks and nice formatting. I would just get a crude outline working first.
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 01:27:39 pm
hello

Let me ask this.. if you mess up on one of the input statements how do you go back up to that statement. with gotos and gosubs that is easy with the way you are explaining you would have to start all over to do that... All the same can you send me a small code snipit on doing what you are suggesting. Please

thanks in advance
Badger
Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 01:34:46 pm
The beauty of INPUT is that before you press Enter to move on you can check what you wrote, but yes surely there will be a screw up any way!

So before you exit the sub you can confirm the whole record is good to send back to main code or eh... start over is definitely easiest but who wants that?

OK I will work on data Entry Form, I had a nice generic one made with EDIT way, way back in 90's with QB4.5

But go ahead and try a crude one as I suggest so you can get to work on saving data to file and loading a records view or making queries or getting on with the whole of the thing. Get that working good then refine later.

Customer ID? will that be the same as record number?
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 01:36:30 pm
Hello

Thanks i want to see that i am willing to learn new tricks i am not that old LOL

Badger
Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 02:42:17 pm
Apology to badger, sorry I missed the place where GOSUB'd to mainloop in reply #3.

Now the RETURN makes more sense.

I hope I can help show how nice working with SUBs are. But remember one main thing, variable values are private to SUB, what happens in a SUB stays in the SUB unless you DIM SHARE variables in main or pass info in and out through arguments to the SUB (or FUNCTION). This is the main difference between a SUB and a GOSUB.
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 02:47:14 pm
Hello

That is ok i figured as much i had to recheck that spot to make sure it was ok being in side the if statement. I understate how they work just not use to using them i have done so visual stuff in vb and vc but quick basic is my choice. I am so glade it has been ported to the 64 bit platform. I just need to see how what you are talking about works.

Badger
Title: Re: memory leak and question
Post by: badger on September 29, 2020, 02:55:40 pm
Hello

i posted i thought i dont think i hit post ugg.

NO problems on the mistake it made me look at that spot to make sure that return should be in that if statement.

I know how subs and functions work i do a little work in vb and vc but my first love is qb 4.5. I am really glad you all ported qb to 64 bit.

badger
Title: Re: memory leak and question
Post by: SMcNeill on September 29, 2020, 04:24:56 pm
A very quick demo of a customer input routine:

Code: QB64: [Select]
  1.  
  2.  
  3. GetRecord
  4. PRINT "This is the record you entered:"
  5. PRINT "Customer Name: "; n(1)
  6. PRINT "Customer Age :"; n(2)
  7. PRINT "Customer Sex :"; n(3)
  8.  
  9.  
  10.  
  11.  
  12.  
  13. SUB GetRecord
  14.     'A simple three field record:  name, age, sex
  15.     'I could use a TYPE with these, but there's not much point in that.  At least not for just a simple demo
  16.     'Instead, I'm just going to use a simple 3 element array, which I dimmed and shared in the main module
  17.     DIM c(3) AS STRING * 1 ' And this is just a simple array to hold a cursor so we know where we're entering data at
  18.     EntryOn = 1 'Start with the first field by default
  19.  
  20.     DO
  21.         IF EntryOn < 1 THEN EntryOn = 3 'same basic error checking to loop around our fields
  22.         IF EntryOn > 3 THEN EntryOn = 1 'same as above
  23.         CLS
  24.         FOR i = 1 TO 3: c(i) = " ": NEXT 'clear all cursors
  25.         c(EntryOn) = CHR$(219)
  26.         PRINT "Customer Name: "; n(1); c(1)
  27.         PRINT "Customer Age : "; n(2); c(2)
  28.         PRINT "Customer Sex : "; n(3); c(3)
  29.         PRINT
  30.         PRINT "<CTRL-ENTER> to finish."
  31.  
  32.         k = _KEYHIT
  33.         SELECT CASE k
  34.             CASE 18432 'up arrow
  35.                 EntryOn = EntryOn - 1
  36.             CASE 20480, 9 'down arrow, TAB
  37.                 EntryOn = EntryOn + 1
  38.             CASE 8 'backspace
  39.                 n(EntryOn) = LEFT$(n(EntryOn), LEN(n(EntryOn)) - 1)
  40.             CASE 65 TO 90, 97 TO 122, 32, 48 TO 57 'A to Z, a TO z, SPACE, 0 to 9
  41.                 n(EntryOn) = n(EntryOn) + CHR$(k)
  42.             CASE 13
  43.                 IF _KEYDOWN(100306) OR _KEYDOWN(100305) THEN 'CTRL is down when we hit enter
  44.                     EXIT SUB
  45.                 ELSE 'CTRL isn't down.  It's just an ENTER by itself.  Move to the next line
  46.                     EntryOn = EntryOn + 1
  47.                 END IF
  48.         END SELECT
  49.         _LIMIT 30
  50.     LOOP
  51.  

One short SUB, which is completely self contained in this case, does all the work for us, and it makes it quite easy to navigate and work with our fields.  I'm thinking this is the style input routine which bplus was talking about above, for use.  :)






Title: Re: memory leak and question
Post by: bplus on September 29, 2020, 05:30:22 pm
Yeah I just finished my 50 liner, I turned it into a FUNCTION in case user wants to cancel or quit an edit so it would return 0, otherwise an accepted edit returns -1

Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain:
  50.     CLS
  51.     FOR i = 1 TO 7 'show what we have
  52.         PRINT i, rec(i, 0); " = "; rec(i, 1)
  53.     NEXT
  54.     PRINT: PRINT "Press a to Accept, n for New, a digit to edit field, or escape to Cancel."
  55.     choice$ = getChar$("aAnN1234567" + CHR$(27))
  56.     IF ASC(choice$) = 27 THEN ' maybe this should be a function
  57.         EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  58.     ELSEIF UCASE$(choice$) = "A" THEN
  59.         customerRec.id = recNumber 'assign the record
  60.         customerRec.first = rec(1, 1)
  61.         customerRec.last = rec(2, 1)
  62.         customerRec.address = rec(3, 1)
  63.         customerRec.city = rec(4, 1)
  64.         customerRec.state = rec(5, 1)
  65.         customerRec.zip = rec(6, 1)
  66.         customerRec.phone = rec(7, 1)
  67.         EditCustomer% = -1: EXIT FUNCTION 'done
  68.     ELSEIF UCASE$(choice$) = "N" THEN
  69.         CLS
  70.         FOR i = 1 TO 7
  71.             PRINT rec(i, 0); " > ";
  72.             INPUT ""; rec(i, 1)
  73.         NEXT
  74.         GOTO doAgain
  75.     ELSEIF INSTR("1234567", choice$) THEN
  76.         CLS
  77.         PRINT rec(VAL(choice$), 0); " > ";
  78.         INPUT ""; rec(VAL(choice$), 1)
  79.         GOTO doAgain
  80.     END IF
  81.  
  82. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  83.     DIM OK AS INTEGER, k$
  84.     WHILE OK = 0
  85.         k$ = INKEY$
  86.         IF LEN(k$) THEN
  87.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  88.         END IF
  89.         _LIMIT 200
  90.     WEND
  91.     _KEYCLEAR
  92.     getChar$ = k$
  93.  
  94.  

EDIT: a last minute change set the cancel to -1, supposed to be 0, fixed.
Updated the record description returned to main code.
Edit2: Got first and last name switched when showing record returned from Function, fixed.

Typically blundering my way to better code ;-))
Title: Re: memory leak and question
Post by: TempodiBasic on September 30, 2020, 05:52:09 am
so we can say from a large main with so much GOSUB/RETURN (in the place of GOTO labels) we get a shrink SUB of Steve!

just to let be more readable for my eyes that code I have toggled few things
Code: QB64: [Select]
  1. CONST cName = 1, cAge = 2, cSex = 3
  2.  
  3.  
  4. GetRecord
  5. PRINT "This is the record you entered:"
  6. PRINT "Customer Name: "; n(cName)
  7. PRINT "Customer Age :"; n(cAge)
  8. PRINT "Customer Sex :"; n(cSex)
  9.  
  10.  
  11.  
  12.  
  13.  
  14. SUB GetRecord
  15.     'A simple three field record:  name, age, sex
  16.     'I could use a TYPE with these, but there's not much point in that.  At least not for just a simple demo
  17.     'Instead, I'm just going to use a simple 3 element array, which I dimmed and shared in the main module
  18.     DIM c(3) AS STRING * 1 ' And this is just a simple array to hold a cursor so we know where we're entering data at
  19.     EntryOn = 1 'Start with the first field by default
  20.  
  21.     DO
  22.         IF EntryOn < cName THEN EntryOn = cSex 'same basic error checking to loop around our fields
  23.         IF EntryOn > cSex THEN EntryOn = cName 'same as above
  24.         CLS
  25.         FOR i = 1 TO 3: c(i) = " ": NEXT 'clear all cursors
  26.         c(EntryOn) = CHR$(219)
  27.         PRINT "Customer Name: "; n(cName); c(cName)
  28.         PRINT "Customer Age : "; n(cAge); c(cAge)
  29.         PRINT "Customer Sex : "; n(cSex); c(cSex)
  30.         PRINT
  31.         PRINT "<CTRL-ENTER> to finish."
  32.  
  33.         k = _KEYHIT
  34.         SELECT CASE k
  35.             CASE 18432 'up arrow
  36.                 EntryOn = EntryOn - 1
  37.             CASE 20480, 9 'down arrow, TAB
  38.                 EntryOn = EntryOn + 1
  39.             CASE 8 'backspace
  40.                 n(EntryOn) = LEFT$(n(EntryOn), LEN(n(EntryOn)) - 1)
  41.             CASE 65 TO 90, 97 TO 122, 32, 48 TO 57 'A to Z, a TO z, SPACE, 0 to 9
  42.                 n(EntryOn) = n(EntryOn) + CHR$(k)
  43.             CASE 13
  44.                 IF _KEYDOWN(100306) OR _KEYDOWN(100305) THEN 'CTRL is down when we hit enter
  45.                     EXIT SUB
  46.                 ELSE 'CTRL isn't down.  It's just an ENTER by itself.  Move to the next line
  47.                     EntryOn = EntryOn + 1
  48.                 END IF
  49.         END SELECT
  50.         _LIMIT 30
  51.     LOOP
  52.  
  53.  
  54.  
Title: Re: memory leak and question
Post by: TempodiBasic on September 30, 2020, 05:53:34 am
So why I must waste only Steve's work, it is also your turn:
in a gray office program it is beautiful to see more colors...
TA-DA!
Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain:
  50.     CLS
  51.     FOR i = 1 TO 7 'show what we have
  52.         COLOR i: PRINT i, rec(i, 0); " = "; rec(i, 1)
  53.     NEXT
  54.  
  55.     PRINT: PRINT "Press ";: COLOR 2: PRINT "a";: COLOR 7: PRINT " to Accept, ";
  56.     COLOR 3: PRINT "n";: COLOR 7: PRINT " for New, a ";
  57.     COLOR 4: PRINT "digit";: COLOR 7: PRINT " to edit field, or ";
  58.     COLOR 5: PRINT "escape";: COLOR 7: PRINT " to Cancel."
  59.     choice$ = getChar$("aAnN1234567" + CHR$(27))
  60.     IF ASC(choice$) = 27 THEN ' maybe this should be a function
  61.         EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  62.     ELSEIF UCASE$(choice$) = "A" THEN
  63.         customerRec.id = recNumber 'assign the record
  64.         customerRec.first = rec(1, 1)
  65.         customerRec.last = rec(2, 1)
  66.         customerRec.address = rec(3, 1)
  67.         customerRec.city = rec(4, 1)
  68.         customerRec.state = rec(5, 1)
  69.         customerRec.zip = rec(6, 1)
  70.         customerRec.phone = rec(7, 1)
  71.         EditCustomer% = -1: EXIT FUNCTION 'done
  72.     ELSEIF UCASE$(choice$) = "N" THEN
  73.         CLS
  74.         FOR i = 1 TO 7
  75.             PRINT rec(i, 0); " > ";
  76.             INPUT ""; rec(i, 1)
  77.         NEXT
  78.         GOTO doAgain
  79.     ELSEIF INSTR("1234567", choice$) THEN
  80.         CLS
  81.         PRINT rec(VAL(choice$), 0); " > ";
  82.         INPUT ""; rec(VAL(choice$), 1)
  83.         GOTO doAgain
  84.     END IF
  85.  
  86. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  87.     DIM OK AS INTEGER, k$
  88.     WHILE OK = 0
  89.         k$ = INKEY$
  90.         IF LEN(k$) THEN
  91.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  92.         END IF
  93.         _LIMIT 200
  94.     WEND
  95.     _KEYCLEAR
  96.     getChar$ = k$
  97.  
  98.  
  99.  
Title: Re: memory leak and question
Post by: TempodiBasic on September 30, 2020, 06:04:51 am
just to follow my friend enemy of GOTO
here an alternative way of GOTO into FUNCTION
Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain = -1
  50.     DO WHILE doAgain
  51.         CLS
  52.         FOR i = 1 TO 7 'show what we have
  53.             COLOR i: PRINT i, rec(i, 0); " = "; rec(i, 1)
  54.         NEXT
  55.  
  56.         PRINT: PRINT "Press ";: COLOR 2: PRINT "a";: COLOR 7: PRINT " to Accept, ";
  57.         COLOR 3: PRINT "n";: COLOR 7: PRINT " for New, a ";
  58.         COLOR 4: PRINT "digit";: COLOR 7: PRINT " to edit field, or ";
  59.         COLOR 5: PRINT "escape";: COLOR 7: PRINT " to Cancel."
  60.         choice$ = getChar$("aAnN1234567" + CHR$(27))
  61.         IF ASC(choice$) = 27 THEN ' maybe this should be a function
  62.             EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  63.         ELSEIF UCASE$(choice$) = "A" THEN
  64.             customerRec.id = recNumber 'assign the record
  65.             customerRec.first = rec(1, 1)
  66.             customerRec.last = rec(2, 1)
  67.             customerRec.address = rec(3, 1)
  68.             customerRec.city = rec(4, 1)
  69.             customerRec.state = rec(5, 1)
  70.             customerRec.zip = rec(6, 1)
  71.             customerRec.phone = rec(7, 1)
  72.             doAgain = 0
  73.             EditCustomer% = -1: EXIT FUNCTION 'done
  74.         ELSEIF UCASE$(choice$) = "N" THEN
  75.             CLS
  76.             FOR i = 1 TO 7
  77.                 PRINT rec(i, 0); " > ";
  78.                 INPUT ""; rec(i, 1)
  79.             NEXT
  80.             doAgain = -1
  81.         ELSEIF INSTR("1234567", choice$) THEN
  82.             CLS
  83.             PRINT rec(VAL(choice$), 0); " > ";
  84.             INPUT ""; rec(VAL(choice$), 1)
  85.             doAgain = -1
  86.         END IF
  87.     LOOP
  88.  
  89. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  90.     DIM OK AS INTEGER, k$
  91.     WHILE OK = 0
  92.         k$ = INKEY$
  93.         IF LEN(k$) THEN
  94.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  95.         END IF
  96.         _LIMIT 200
  97.     WEND
  98.     _KEYCLEAR
  99.     getChar$ = k$
  100.