0

Hi this is a questing from some cs home work. Our task was to take a ada spec file and write the code for the functions and the main program, I have already done this in c++ but am having problems doing it in ada.

here is my code:

```
package stackPkg is
MAX : constant integer := 100; -- Maximum capacity
type stack is private;
overflow, underflow : exception; -- Stack overflow/underflow conditions
function empty(S : in stack) return boolean;
-- Returns true if stack S is empty.
function topItem(S : in stack) return integer;
-- Returns the top element of stack S.
-- Raises the underflow exception if S is empty.
procedure push(S : in out stack; item : in integer);
-- Pushes the item on top of stack S.
-- Raises the overflow exception if S is full.
procedure pop(S : in out stack; item : out integer);
-- Pops the top element of stack S and stores it in item.
-- Raises the underflow exception if S is empty.
private
type stack is array(positive range 1..MAX) of integer;
top : natural := 0; -- array index of the top element
end stackPkg;
```

This is what was already supplied to me and here is what I wrote to go with the above spec file.

```
package body stackPkg is
procedure push (S : in out stack; item : in integer) is
begin
S (top):= item;
top:= top + 1;
--pushes the input on to the stack.
end push;
--------------------------------------------------------------------------------
procedure pop(S : in out stack; item : out integer) is
begin
item := top;
top:= top -1;
end pop;
--------------------------------------------------------------------------------
function topItem(S : in stack) return integer is
begin
return top;
--wanted to do stack(top) but that didn't work.
end topItem;
--------------------------------------------------------------------------------
function empty(S : in stack) return boolean is
begin
if(top=0) then
return true;
else
return false;
end if;
end empty;
--------------------------------------------------------------------------------
end stackPkg;
```

and the main is:

```
with ada.Text_IO, stackpkg, ada.integer_text_io;
use stackPkg, Ada.Text_IO ,ada.integer_text_io;
procedure main is
s: stack;
temp: integer;
begin
for Base in 1..20 loop
push(s, Base);
end loop;
push(s, 5);
put_line("The stack will now be printed.");
new_line;
while empty(s)=false loop
pop(s, temp);
new_line;
put(temp);
end loop;
--Pop the stack one more time to generate an error.
pop(s, temp);
end main;
```

After much dificulty I got this to complie but now when I run it I get the error:

raised CONSTRAINT_ERROR : stackpkg.adb:5 index check failed

How can I fix this and any other errors that are in my code? Any help would be appreciated since this is my first program in this language.