Page 1 of 1

Bug: "Out of String Space" bug Found in "Palindromotron" program

Posted: Sun Dec 11, 2016 6:19 am
by Webbmaster1
Tutorial:(Beginners & newbies Edition) by Neo Deus Ex Machina
Chapter: Modifiers & Structure (IV)
Program: Chapter Program "Palindromotron":

Here is the full Code as presented in the pdf:

Code: Select all

Again:
CLS
COLOR 15, 1
PRINT “==================================”
PRINT “   ===========      ===========”
PRINT “      === Palindromotron ===”
PRINT “   ===========      ===========”
PRINT “==================================”
COLOR 7, 0
PRINT “This program is capable of reversing”
PRINT “words, sentences or entire texts.”

DIM TheString AS STRING, EndString AS STRING
INPUT “Please enter some:”, TheString
TheString = LTRIM$(RTRIM$(TheString))
IF LEN(TheString) = 0 THEN GOTO Again

GOSUB reversestring
PRINT “This is it, reversed: ”; EndString
PRINT
EnterAgain:
PRINT “Do you want to enter some text again? (y/n)”
INPUT “(y/n):”, choice$
Choice$ = LTRIM$(RTRIM$(UCASE$(choice$)))
IF Choice$ = “Y” THEN GOTO Again
IF Choice$ = “N” THEN END
‘the program will only get here if Choice$ ≠ “Y” AND Choice$ ≠ “N”
GOTO EnterAgain

Reversestring:
‘this function can actually be made much better using FOR or WHILE
‘but since you haven‘t had them yet, I do it with GOTO

NextChar:
CurrentPos = LEN(TheString)
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos – 1	‘decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar
RETURN
Bug: The problem is with this line CurrentPos = LEN(TheString) in the NextChar: sub-routine. It is causing the string buffer to continuously fill up with the letter at the end of the string, until you get the error warning, "out of string space.

Fix: Set CurrentPos only once, before the NextChar sub-routine:

Code: Select all

CurrentPos = LEN(TheString)

NextChar:
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos – 1	‘decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar

Re: Bug: "Out of String Space" bug Found in "Palindromotron" program

Posted: Sun Dec 11, 2016 1:33 pm
by burger2227
Where did you find that code? Here?

The string length never changes each GOTO...that caused Out of String Space error.

Your comment apostrophe was not correct character '

Code: Select all

Again:
CLS
COLOR 15, 1
PRINT "=================================="
PRINT "   ===========      ==========="
PRINT "      === Palindromotron ==="
PRINT "   ===========      ==========="
PRINT "=================================="
COLOR 7, 0
PRINT "This program is capable of reversing"
PRINT "words, sentences or entire texts."

DIM TheString AS STRING
DIM EndString AS STRING
INPUT "Please enter some text:", TheString
TheString = LTRIM$(RTRIM$(TheString))
IF LEN(TheString) = 0 THEN GOTO Again

GOSUB Reversestring  
PRINT "This is it, reversed: "; EndString
PRINT
EnterAgain:
PRINT "Do you want to enter some text again? (y/n)"
INPUT "(y/n):", Choice$

Choice$ = UCASE$(LTRIM$(RTRIM$(UCASE$(Choice$))))
IF Choice$ = "Y" THEN GOTO Again ELSE END
                                               
Reversestring:
'this function can actually be made much better using FOR or WHILE
'but since you haven`t had them yet, I do it with GOTO
CurrentPos = LEN(TheString)

NextChar:
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos - 1 'decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar
RETURN  ' used by Reversestring only as a GOSUB