1

The program below is suppose to get an input of Characters from the user and put it into an Record with the subprogram Procedure Get(Tecken : out Record_Type). The last step is to Print it in the Terminal with another subprogram Procedure Put(Chars : in Chars_Array).

The issue I'm facing is the actual output of the Characters into the Terminal where I get:

Picture of the Output

procedure Poster is

type Char_Array is                            -- Fält/Array
    array (1..256) of Character;

type Record_Type is                           -- Post/Record
    record
        Chars : Char_Array;
        Length : Integer;
    end record;

procedure Get(Tecken : out Record_Type) is   -- Character input

begin

    for I in 1..256 loop
        Get(Tecken.Chars(I));
        exit when End_Of_Line;
    end loop;

end Get;      

Here is the subprogram that prints the Characters from the Array. I'm confused when it comes to Records and Arrays, but I'm thinking that I need an Array as a inparameter since I can't loop through Records.

procedure Put(Chars : in  Char_Array) is   -- Character output

begin

    for I in 1..256 loop
        Put(Chars(I));
    end loop;

end Put;   

Here is another bump in the road where I use an inparameter "Chars" the Put-Call. Does the Char_Array recognize the input in "Tecken" ?

Tecken : Record_Type;
Chars : Char_Array; 

begin

    Get(Tecken);
    Put(Chars);

end Poster;
R.Englund
  • 21
  • 5

1 Answers1

1

There’s a bit of a warning from the compiler:

poster.adb:31:04: warning: variable "Chars" is read but never assigned

This is because Get writes to Tecken.Chars; the Chars you pass to Put has nothing to do with it, and is filled with random data.

You say "I'm thinking that I need an Array as a inparameter since I can't loop through Records". True, you can’t loop through a record, but you can loop through an array that is part of a record.

If you say

Put(Tecken.Chars);

things work better, though you still get garbage after the actual input. You might expect even better if you say

   Put(Tecken.Chars (1 .. Tecken.Length));

but

$ ./poster 
0123456789

raised CONSTRAINT_ERROR : poster.adb:36 range check failed

which is because Get doesn’t actually set Tecken.Length.

After fixing this[*], this should do the trick

procedure Put (Tecken : in  Record_Type) is
begin
   for I in 1 .. Tecken.Length loop
      Put (Tecken.Chars(I));
   end loop;
end Put;

Now, the call is just

Put (Tecken);

[*] It’s not as easy to fix Get as you might hope, since when End_Of_Line is true the character returned is the last one on the line.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • When I add Put(Tecken.Chars(1..Tecken.Length)) to the call, and the subprogram to "procedure Put(Tecken : in Record_Type) is begin – R.Englund Oct 29 '17 at 15:33
  • Ignore the above comment. I added the things to told me to in both the Call and inside the subprogram but I get the Error-messages: "expected type "Record_Type" defined at line 9" "found type "Char_Array" defined at line 6" The compiler is complaining about Record_Type, and Tecken.Chars is clearly a Record_Type, or am I wrong? – R.Englund Oct 29 '17 at 15:44
  • After altering the definition of `Put`, the call should just be `Put (Tecken);`. `Tecken` is a `Record_Type`, and `Tecken.Chars` is a `Char_Array`. – Simon Wright Oct 29 '17 at 15:54