What I whant is to define a array of Strings in ADA.
I'm trying to execute this code:
type String is array (Positive range <>) of Character;
type lexicon is array(1..7) of String(1..20);
nomFumadors : lexicon := ("Macia","Xisco","Toni","Laura","Rocky","Paz");
nomNoFumadors : lexicon := ("Marina","Marta","Joan","Africa","America");
And the compiler sais:
warning:wrong length for array of subtype of "String" defined at line 42
My line 42 is :
type lexicon is array(1..7) of String(1..20);
But compailer sais the warning is in line 43 and 44: what are these:
nomFumadors : lexicon := ("Macia","Xisco","Toni","Laura","Rocky","Paz");
nomNoFumadors : lexicon := ("Marina","Marta","Joan","Africa","America");
Can somebody help me with that?
Thanks!!
CodePudding user response:
You declared your array to hold Strings of length 20. The String literals you give are less than 20 Characters long. Hence the error.
You seem to be looking for a string type that contains a maximum of 20 characters. This is provided in Ada.Strings.Bounded:
package Max_20_String is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
use Max_20_String;
type Lexicon is array (1..7) of Bounded_String; -- from Max_20_String
nomFumadors : Lexicon := (To_Bounded_String ("Macia"),
To_Bounded_String ("Xisco"),
To_Bounded_String ("Toni"),
To_Bounded_String ("Laura"),
To_Bounded_String ("Rocky"),
To_Bounded_String ("Paz"));
To get back a String from a Bounded_String, use e.g. To_String (Lexicon (2)).
CodePudding user response:
Another option is Unbounded_String (as its name suggests, length is variable and unlimited):
with Ada.Strings.Unbounded;
procedure Fumador is
use Ada.Strings.Unbounded;
subtype VString is Unbounded_String;
function " " (Source : in String) return VString renames To_Unbounded_String;
type Lexicon is array (Integer range <>) of VString; -- Unknown number of people.
nomFumadors : Lexicon := ( "Macia", "Xisco", "Toni", "Laura", "Rocky", "Paz");
nomNoFumadors : Lexicon := ( "Marina", "Marta", "Joan", "Africa", "America");
begin
null;
end;
