QBasic Bits

Introduction

This is the page where I've put items that don't fit onto any other page.

Running QBasic from a batch file &
Running a QBasic Program Without Showing The Editor

Here's a handy trick to run a QBasic program without showing the editor. Write a batch file with the line :-

d:\qbasic\qbasic /run yourprog.bas

Where d:\qbasic\qbasic is the path to qbasic.exe

and yourprog.bas is the name of the program you want to run

Instead of using END in your program, which will cause the editor to open when the program has finished, use SYSTEM which exits the program and closes the editor.

MID$

QBasic contains a statement, MID$, which can replace part of a string with another. From the help file the usage of it is...

The MID$ statement replaces part of a string variable with another string.

MID$(stringvariable$,start%[,length%])=stringexpression$

stringexpression$ - The string from which the MID$ function returns a substring, or the replacement string used by the MID$ statement. It can be any string expression.
start% - The position of the first character in the substring being returned or replaced.
length% - The number of characters in the substring. If the length is omitted, MID$ returns or replaces all characters to the right of the start position.
stringvariable$ The string variable being modified by the MID$ statement.

and the example given is...

a$ = "Where is Paris?"
PRINT MID$(a$, 10, 5) 'Output is: Paris
Text$ = "Paris, France"
PRINT Text$ 'Output is: Paris, France
MID$(Text$, 8) = "Texas "
PRINT Text$ 'Output is: Paris, Texas

What the help file doesn't mention is that the part of the original string replaced by stringvariable$ must be of the same length. In the example given by Microsoft, if the trailing space in the line MID$(Text$, 8) = "Texas " is removed then the output is not Paris, Texas but Paris, Texase

'MidEnq.bas Ray Thomas 10th April 2006

'Program to investigate the MID$ function
DIM origStr AS STRING DIM repStr AS STRING origStr$ = "elephant" repStr$ = "ant" CLS PRINT MID$(origStr$, 1, 3) = repStr$ PRINT origStr$ + " - antphant expected" origStr$ = "elephant" MID$(origStr$, 1) = repStr$ PRINT origStr$ + " - ant expected" origStr$ = "elephant" MID$(origStr$, 1, 2) = repStr$ PRINT origStr$ + " - anephant expected" origStr$ = "elephant" MID$(origStr$, 1, 4) = repStr$ PRINT origStr$ + " - anthant expected" END

The output of most of the substitutions is antphant. It doesn't matter if the the entire original string is replaced or if the amount of the original to be replaced by the replacement is longer than the replacement, MID$ used to substitute strings does not work as it is expected to - at least by me. The only way it appears to work properly is if the length specified for the original string to be replaced is the same length or shorter than the replacement string.

MOD :-

MOD returns the modulus of two numbers. What this means that it divides one number by another and the result is the remainder of the division. In the expression Num = 24 MOD 8 then Num will be 0, 8 goes into 24 three times with no remainder. Num = 24 MOD 9 will result in Num being 6. This is because 9 will go into 24 twice, with remainder 6. If the second number is greater than the first then MOD simply returns the first number. The expression Num = 24 MOD 25 then Num will be 24.

When I writing the page on numbers and number series I came across a problem I hadn't seen before. I didn't realise it before, but the MOD command has a limit to what it can test for. If either of the numbers is greater than 2,147,483,647 or smaller than -2,147,483,647 then QBasic will give an overflow error (Error 6). This seems to be because when the language was being written MOD was designed to be used only with LONG integers. Another manifestation of this is in the fact that MOD rounds off the decimal  numbers. This can be seen very easily by using Num = 6 MOD 3.1 this results in Num = 0. This is the result given when 6 MOD 3 is used. When Num = 6 MOD 3.9 is used then Num results in being 2. This is the same as 6 MOD 4.

The following code shows the method I came up with to replace the MOD operator that can handle large numbers and decimals.

DIM Num1 AS DOUBLE
DIM Num2 AS DOUBLE
DIM NewNum AS DOUBLE

NewNum = (Num1 / Num2) * 1000
NumStr$ = STR$(NewNum)

IF RIGHT$(NewStr$, 3) = "000" THEN
	PRINT "MOD would be TRUE"
	PRINT "MOD would be FALSE"
END IF

There is one circumstance under which this method will fail, and produce FALSE even if it were TRUE. This occurs when the difference between the two numbers is about 10,000,000,000. This is because NumStr becomes 1D+x when it's too big or small to handle in ordinary notation. You can try this yourself in the program MODRep

'ModRep Ray Thomas April 2002

DIM Num1 AS DOUBLE
DIM Num2 AS DOUBLE
DIM NewNum AS DOUBLE

CLS
PRINT
PRINT "Just press enter to exit the program"
PRINT
PRINT "One way to make this program fail is when the difference between"
PRINT "the two inputs is great than around 10,000,000,000"
PRINT "Try Num1 = 99990000000000 and Num2 = 10"
PRINT

DO
   INPUT "   Enter the numerator     ", Num1
IF Num1 = 0 THEN END PRINT INPUT "   Enter the denominator   ", Num2 IF Num2 = 0 THEN END PRINT PRINT NewNum = (Num1 / Num2) * 1000 NumStr$ = STR$(NewNum) NewNum = NewNum / 1000 IF RIGHT$(NumStr$, 3) = "000" THEN PRINT " TRUE :- "; ELSE PRINT " FALSE :- "; END IF PRINT Num1; "/"; Num2; "="; NewNum; "NumStr$ ="; NumStr$
PRINT PRINT LOOP UNTIL Num1 = 0 END

The programs on this page, like all the programs written for this site, can be downloaded from the Downloads page.

This page created 10th April 2002, last modified 10th April 2006


GoStats stats counter