Computer Science Canada

Array Subscript Error

Author:  Dragon20942 [ Fri Nov 25, 2011 11:19 pm ]
Post subject:  Array Subscript Error

What is it you are trying to achieve?
I am attempting to create a text file interactive save/load system for any future projects


What is the problem you are having?
Using the following file information: 2.4,6.3,4.2,8.4,0.0,0.0,0.0,0.0,0.0,0.0,
I am able to gain the desired results. However, once any post "." value becomes more than 1 digit, I get the following error message: "Array subscript is out of range." on line 26.


Describe what you have tried to solve this problem
Line 26 deals with the pre "." integer, therefore the value of that integer should not affect line 26, but line 31. I have thought about this for hours, and changed the code slightly.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


%File Syntax: item.nItems,item.nItems etc.

type invSlot : %an inventory slot holds the type of item, and the amount
    record
        item : int
        nItems : int
    end record

var inventory : array 1 .. 10 of invSlot %10 inventory slots in inventory
var stream : int
var placeHolder := 1 %the string index of the beginning of each extracted value in the file
var slotNumber := 1 %the slot in which information is stored
var data : string %extracted information

for i : 1 .. 10
    inventory (i).item := 0 %init
    inventory (i).nItems := 0 %init
end for

open : stream, "save1.txt", get
get : stream, data

for i : 1 .. length (data)
    if data (i) = "." then %if a "." is detected
        for j : placeHolder .. i - 1  %mark from the placeHolder to just before the "." (extracted values between "." and ",", and vice versa)
            inventory (slotNumber).item := strint (intstr (inventory (slotNumber).item) + (data (j))) %item becomes length(placeholder) to length (i)
            placeHolder := i + 1 %next variable begins after "."
        end for
    elsif data (i) = "," then %repeated for "," but for inventory(i).nItems instead
        for j : placeHolder .. i - 1
            inventory (slotNumber).nItems := strint (intstr (inventory (slotNumber).nItems) + (data (j)))
            placeHolder := i + 1
            slotNumber += 1 %after every "," another slot is set
        end for
    end if
end for

for i : 1..10
put inventory (i).item , " x "..
put inventory (i).nItems
end for



Please specify what version of Turing you are using
4.1.1

Author:  Tony [ Fri Nov 25, 2011 11:28 pm ]
Post subject:  Re: Array Subscript Error

Dragon20942 @ Fri Nov 25, 2011 11:19 pm wrote:
I get the following error message: "Array subscript is out of range." on line 26.

Right before the faulty array access, print out the index that is attempted to be used. The actual value of the index might give you a clue as to why it's not what you expect it to be.

Author:  Dragon20942 [ Fri Nov 25, 2011 11:42 pm ]
Post subject:  Re: Array Subscript Error

Turing:

  for j : placeHolder .. i - 1
            inventory (slotNumber).nItems := strint (intstr (inventory (slotNumber).nItems) + (data (j)))
            placeHolder := i + 1
        end for
         slotNumber += 1 %after every "," another slot is set


Thanks Tony!


: