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

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Webbmaster1
Newbie
Posts: 1
Joined: Sun Dec 11, 2016 5:40 am

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

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

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

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply