Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help again!

Status
Not open for further replies.

qbasicking

Programmer
Aug 19, 2001
628
US
I had a program working, but then i moved soem things around to make it work faster, but then it stopped working again. It's not a programming error, because it starts to run fine.
When it the main scripting language it does some weird things. Here's the code that giving me problems(it says that the subscripts are out of range)

IF comm$ = "IF=TO" AND variable$(aa,prog) =
variable$(bb,prog) THEN place(prog) = cc-1

this command is kinda like IF = THEN GOSUB. Qbasic tells me that one (or more) of the subscripts are out of range, but even it they were, it shouldn't even matter. i wrote the program that it is having trouble running, and I didn't even use the "IF=TO" command.
All of the strings are declare correctly, i looked over everything, and it looks right.
It also has a problem with

IF comm$ = &quot;IF~TO&quot; AND variable$(aa,prog) <>
variable$(bb,prog) THEN place(prog) = cc-1
 
If variables$ and place aren't defined as arrays with as many elements as (aa,prog), (bb,prog) and (prog) then this will certainly be causing the problem.

When QBasic starts, an undefined aray contains 10 elements. Once you reference a subscript higher than 10 then you WILL get the &quot;subscript out of range&quot; error. This error does matter and is a fatal program error, which means it just stops working.

Try this code, it will give the appearance of running. Check the screen with F4 to prove it has done something. It will show Count as being 10, because as soon as Count reaches 11 the limit of the undefined array has been reached.

FOR Count = 1 TO 15
MyArray(Count) = Count * 2
PRINT MyArray(Count), Count
NEXT Count

I think this is what is happening in your program, and that is why it's giving the appearance of working, then stopping.

The code above will work if you precede it with
DIM MyArray(15) AS INTEGER

Ray
 
Just to add.. if you are using very large arrays try using the /ah line command when activating QB. Although, I don't believe this option is available in QB 1, but is available in QB 4 - 7. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
Brisray: I did declare the strings, if I hadn't Qbasic would pick it up before it even starts to run the .bas program.

I declared these arrays in this way
DIM SHARED variable$(20,10)

but even so i don't think it should matter. When qbasic is running through the program, it should read
IF comm$ = &quot;IF=TO&quot; and then stop and goto the next line, so it shouldn't even read the part of the code with the strings.

Does my use of the AND statement have something to do w/ it, should I chaneg it to
IF comm$ = &quot;IF=TO&quot; THEN IF variable$(aa,prog)...?

Also miggyd, I guess I didn't specify this earlier, but the array is fairly small and you're right, 1.1 doesn't support that option and that's what i am using
 
Here's what the helpfile has to say about ERROR 9 - Subscript out of range(QBasic v1.1)

An array element was referred to with a subscript that was
outside the dimensions of the array, or an element of an
undimensioned dynamic array was accessed. Check that the
array is dimensioned and check the bounds of the array.

You may also get this error if the array size exceeds 64K.
Reduce the size of the array.

ERR code: 9

According to this, the error code is definately connected to your arrays.

Ray
 
I've also had another thought, are you trying to run QBasic commands in strings?

something like this :-

StrCmmd$ = &quot;GOSUB Prn&quot;

StrCommd$
END

Prn:
PRINT &quot;Now I'm here&quot;
RETURN

This most definately will not work.

Also something else, when QBasic reads an IF statement It checks the entire statement, not just the first condition.
You can check this yourself using something like the code below.

DIM MyArray(5)
Strng$ = &quot;Apples&quot;

IF Strng$ = &quot;Oranges&quot; AND MyArray(22) = 123 THEN GOSUB Prn
END

Prn:
PRINT &quot;Now I'm here&quot;
RETURN

The code above gives ERROR 9 - &quot;Subscript out of range&quot;

According to your thinking the program won't read the MyArray(22) part as Strng$ = &quot;Oranges&quot; is already false. QBasic has to read the entire condition before it decides if the statement is true or false.

Ray
 
I just reread your second post, and the answer is yes, you are right in your thinking. If you use the nested IF it will work. The code below will work, even if MyArray(22) is well outside the limit I set for MyArray.

DIM MyArray(5)
Strng$ = &quot;Apples&quot;

IF Strng$ = &quot;Oranges&quot; THEN
IF MyArray(22) = 123 THEN GOSUB Prn
END IF

END

Prn:
PRINT &quot;Now I'm here&quot;
RETURN
 
Just a thought. If &quot;subscript out of range&quot; is calling beyond the scope of the array <thoughts running away....humm, kind of like brisray mentioned...thoughts coming back> then

if OPTION BASE 1
AND MyArray = (10,10)
and my prg calls MyArray(0,0) OR MyArray(11,11) then
I should get the range error.

QbasicKing:

1) Does this occur on first run or after a few?

2) Have you created a break line (pressed F9) and confirmed the variables are valid prior to line execuition through either the immediate window or produced a TXT file for review?
--MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
Even more thoughts :-

sorry about all these posts, but things keep springing to mind. It was a question MiggyD asked that started this thought off.

The first time you run the program does it work then? The second time you run it (say, after an error, or you stopped the program to check the variables) does it stop working?

The reason I ask is because sometimes when you restart a program the variables are not completely cleared. I recently wrote a program that should have been able to change a number from any number base to any other. When I first run the program it worked fine, but the second time I ran it, it returned some very strange results. An example, the first time the program ran it made the binary of 5, 101 which is correct, the second time it made it 1010, then 1011, then 10100 etc. etc.

I finally tracked this down to not all the variables I was using being cleared properly, easpecially after I'd stopped it to check those same variables.

It's probably nothing to do with your program, which I still think is something to do with your arrays, but it's just something else that you might like to think about. Especially if you are using a counter or something to keep track of where in the array it's supposed to be.

Ray
 
brisray: no, I am not trying to run QBasic commands, it's got it's own executer, with it's own language. The variables are all cleared: If you declare them using DIM qb automatically clears them, but if you don't, then they might not be cleared.
also, you are wrong on one thing, when qb reads a IF command, it only reads the conditional, try writing something using a bunch of IF-THEN statements and follow it using F8. Thanks for clearing up about the use of the AND statement.

Miggyd: It did this the first time.

What struck me as really odd, is that I had the program working, without any errors for a while. But then I moved some parts around to make it faster, I didn't add or delete ANYTHING, just moved stuff. Then it started giving me problems.

If i missed any points posted above, remind me again, there was alot of info up there
 
It just dawned on me whats wrong. Thanks to brisray. If
IF comm$ = &quot;IF=TO&quot; AND variable$(aa,prog)... doesn't work but
IF comm$ = &quot;IF=TO&quot; THEN
variable$(aa,prog)... does when aa is outside the string limit, this is the problem.

Explaination: My program is using the variables aa,bb,cc...for a wealth of commands, including some higher maths commands, so aa could easily be in the thousands, or more likely, will not be an integer, so thats why the first example doesn't work.
Thanks
 
Good job. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top