Lance
Lance

Reputation: 13

Ada - Stacked records?

Basically I'm trying stack a record (1 record is 2 string variables, and there's 3 of them but I'd like to stack at least 1 for the time being). I'm thinking the setup would like something like this for my algorithm:

Client: Values are given to strings in record, procedures for stack are called (ie, push, pop, display) Package: Record is declared, items are pushed/popped onto stack, display stack.

I'm having trouble in general. I tried keeping it all local. It does open the file and read in the strings (tried it out reading in an integer value, works fine), this I have tested in the client program using a similar setup (instead of a record I stored it to a string of length 40). However when I'd go to output it all I'd get is a bunch of random symbols (such as ╤cß≈Ä), no words like the file contained.

Here are my code fragments:

Package spec:

StackMaximum: constant integer := 10;
   TYPE StackItem IS Record
str1: string (1..20);
str2: string (1..20);
 end record;

   type Stack is PRIVATE

    PROCEDURE Push (Item: IN StackItem; AStack: IN OUT Stack);
    PROCEDURE display (AStack : in Stack);

Package body:

procedure Push (Item: in StackItem;
            AStack: in out Stack) is
begin
 if AStack.Top < StackMaximum then
     AStack.Top := AStack.Top + 1;
     AStack.Store(AStack.Top) := Item;
 else
     raise StackOverflow;
 end if;
END Push;

    procedure display(AStack: in stack) is
  BEGIN

     FOR I IN 1..AStack.Top LOOP
        Put(AStack.Store(I.lastname));
     END LOOP;
  END display;

    PRIVATE
    type int_arry is array (1..StackMaximum) of StackItem;
    type Stack is record
        Store: int_arry;
        Top: integer range 0..StackMaximum;
    END RECORD;

Client:

Lt:           Integer;
New_Stack2:    Stack;
A:            StackItem;
Stackitems:   Ada.Text_IO.File_Type;

   Get_Line(File => Stackitems, Item => A.str1, Last => Lt);
   Get_Line(File => Stackitems, Item => A.str2, Last => Lt);

   Push(A, New_Stack1);
   display(New_Stack1);

File (Only contains "This..var."):

This is the test input for the file var.

Any suggestions for what I'm doing wrong with this part? Also here's my other setup where I kept it all local:

Client:

Lt:           Integer;
AB:           String(1..40);
New_Stack2:    Stack;
A:            StackItem;
Stackitems:   Ada.Text_IO.File_Type;

begin

   Get_Line(File => Stackitems, Item => AB, Last => Lt);

   Put(item=> AB);

end;

This is what got me all those symbols. But it is reading in the file, I just have no idea why I'm getting the bad output.

Upvotes: 1

Views: 351

Answers (2)

Rommudoh
Rommudoh

Reputation: 1804

If you use Get (or Get_Line) on something that might be shorter than the variable was defined, you have to store the length.

You already use the variable Lt for this. Now you have to limit the variable in your Put call: Put(item => AB(1 .. Lt));

Upvotes: 0

Simon Wright
Simon Wright

Reputation: 25491

Perhaps in your definition of type Stack you should initialize Top to 0?

type Stack is record
   Store: int_arry;
   Top: Integer range 0 .. StackMaximum := 0;
end record;;

Upvotes: 1

Related Questions