user14236936
user14236936

Reputation:

Creating array with 256 characters?

In this task, I must define the data type String_Type, which must be able to store text. One String_Type should contain two sub-variables, Char_Array, a field of 256 characters, and an integer Length that tells how much of the field is currently being used. Note that String_Type is something completely separate from String which is already defined in Ada. I have tried to write some code for this task but it seems that I have not understood what this task really wants. My code:

with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;

procedure data is
type String_type is
  record
   array (Positive range <>) of Character;  --Error: Component declaration expected
     Heltal_length: Integer;
  end record;
 P1: String_type;

begin
Put("Write your characters: ");
Get(P1.Char_array);
Put(P1.Char_array);

end data;

Upvotes: 1

Views: 452

Answers (2)

Jere
Jere

Reputation: 3641

As Trashgod mentioned, you cannot just define a record field as an array anonymously like you did in your provided example. Additionally, you need to name your field, which you didn't. Ada is a "strongly typed" language meaning it relies heavily on knowing the type and there are many rules that require this (there are some relaxed rules, but this is not one of them).

First I would recommend creating a numeric type just for your string's length. You string can have anything from no letters to all 256 and everywhere inbetween, so I suggest:

subtype String_Length is Integer range 0 .. 256;

Next you need to create an array type for your String_Type record. You can use your newly declared length type to help bound your array with the bonus that if you want to change the max size later, all you have to do is change the String_Length type definition and your String_Type will automatically adjust the max length.

type Character_Array is array (1..String_Length'Last) of Character;

Now you can use these to make a field in your String_Type:

type String_Type is record
    Char_Array  : Character_Array;
    Length      : String_Length := 0;
end record;

This gives you a nice string type that checks for bad length values.

One side note, I noticed you are trying to use the Get procedure to get both the length and then the character array in your record. I would recommend instantiating an instance of the generic package Ada.Text_IO.Integer_IO(String_Length) and using the Get procedure from that package to read the string length. Reading the String is a bit trickier since you are using your own custom type and the Get function expects an Ada String, but that wasn't part of your original question, so hopefully you can work that through and figure out an algorithm. I would recommend using a dummy variable of the Ada string type (sized to match your expected string length) and then use a loop to copy it over to your Char_Array variable.

Upvotes: 3

Mark
Mark

Reputation: 153

Something like this seems to set out the type you describe

procedure Main is

   type Char_Array_Index is range 1 .. 256;
   type Char_Array is array (Char_Array_Index) of Character;

   type String_Type
     (Size : Char_Array_Index := Char_Array_Index'First) is
      record
         Length : Char_Array_Index;
         Data   : Char_Array;
      end record;

begin
   --  Insert code here.
   null;
end Main;

Note the discriminant part parameterising the record. Although not necessarily for strings, this is quite a common pattern in Spark/Ada, when you know you have to allocate some space but you're not sure how much you'll actually need.

The question did say/imply distinct from String, but this seems unlikely in a real world application, so perhaps using subtypes is a better solution

procedure Main is

   subtype Char_Array_Index is Positive range 1 .. 256;
   subtype Char_Array is String (Char_Array_Index);

   type String_Type
     (Size : Char_Array_Index := Char_Array_Index'First) is
      record
         Length : Char_Array_Index;
         Data   : Char_Array;
      end record;

begin
   --  Insert code here.
   null;
end Main;

Using subtypes like this means interoperability with the native String type becomes much easier, so you should be able to use Ada.Text_IO's Get and Put subprograms without too much bother.

EDIT: both these examples do not support an empty Data field, they must always have a minimum length of Char_Array_Index'First, and initialisation of Data needs to be considered appropriately.

Upvotes: 1

Related Questions