
-----------------------------------
swiffer
Wed Apr 21, 2004 9:10 pm

stack demo
-----------------------------------
I'm making a program that demonstrates how a stack works. The program is a series of rectangles stacked on top of each other. When the program performs a 'push', a rectangle is colored and each rectangle continues to be colored when the user hits a certain key. And the program performs a 'pop' when the user enters another key and makes the current rectangle disappear. Here's my code, which doesn't work. Can anyone help me/point out my mistakes? Thanks :shock: 


import GUI
GUI.SetBackgroundColor (purple)
setscreen ("nocursor")

var x := 280
var y := 320
var vx, vy := 40
var top : int := 1
var contents : array 1 .. 8 of int := init (0, 0, 0, 0, 0, 0, 0, 0)
var character : string (1)

var font : int
font := Font.New ("mono:9")
Draw.Text ("Press A to push or D to pop", 20, 380, font, yellow)


procedure push
    var s : int := 8
    top := top + 1
    contents (top) := s
end push

procedure pop
    var s : int := 8
    s := contents (top)
    top := top - 1
end pop

/*Drawing the squares */
for Squares : 1 .. 8
    drawbox (x, y, x + 90, y + 40, 3)   % Duplicate original square 8 times
    y -= vy     % Create a vertical pattern for squares
end for


loop
    getch (character)
    if character = "a" or character = "A" then
        push
        drawfillbox (x, y, x + 90, y + 40, 3)
    elsif character = "d" or character = "D" then
        pop
        drawfillbox (x, y, x + 90, y + 40, purple)
    end if
end loop
[/quote]

-----------------------------------
AsianSensation
Wed Apr 21, 2004 9:27 pm


-----------------------------------
ok, you declared a couple of local variables in your procedure. I don't see how they work, because as soon as you call push or pop, s resets, and I don't see where you are using s any where else.

you get the array subscript out of range error, that's because you never checked to see if your subscript for the array is too low or too high. You should include this before you add or subtract 1 from top.

if top - 1 