Reputation: 51
I am trying to pass my array of integers called "Arr" to a function "collect". inside of collect, I am collecting an int from the user. once the user puts in 100 ints or gives a value of 0 or a negative number the array will stop collecting. What am I doing wrong?
with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO;
procedure helloworld is
procedure PrintHelloWorld is
begin
Put_Line ("Enter Integers, up to 100 individual ints.");
end PrintHelloWorld;
function collect (A : array) return array is
A: array (1 .. 100) of integer;
begin
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
while I > 0 loop
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
end loop;
return A;
end collect;
procedure printArr is
begin
for j in Arr'range loop
Ada.Text_IO.Put_Line(Integer'Image(Arr(j)));
end loop;
end printArr;
Arr: array (1 .. 100) of integer;
Arr := (others => 0);
I: Integer;
begin
Put_Line ("Hello World!");
PrintHelloWorld;
Arr := collect(Arr);
printArr;
end helloworld;
Terminal errors:
gnatmake helloworld.adb
x86_64-linux-gnu-gcc-8 -c helloworld.adb
helloworld.adb:13:26: anonymous array definition not allowed here
helloworld.adb:13:46: reserved word "is" cannot be used as identifier
helloworld.adb:15:08: missing ")"
gnatmake: "helloworld.adb" compilation error
Upvotes: 1
Views: 1266
Reputation: 4198
Here you are; I've also made it so you can add negative numbers.
(If that's not what you want, change the element type of Vector to Natural
or Positive
as desired.)
Note that the --'
comments are a workaround to the buggy syntax highlighter.
Pragma Ada_2012;
Pragma Assertion_Policy( Ignore );
Pragma Restrictions( No_Implementation_Aspect_Specifications );
Pragma Restrictions( No_Implementation_Pragmas );
With
Ada.Strings.Equal_Case_Insensitive,
Ada.Text_IO.Text_Streams;
Procedure Example is
-- Counting and indexing types.
Type Count is range 0..100;
Subtype Index is Count range Count'Succ(Count'First)..Count'Last; --'
-- Array-type; its range is constrained by Index.
Type Vector is Array(Index range <>) of Integer;
-- Here we build the input-vector.
Function Get_Input return Vector is
-- Here we get user-input; we pass our working-set as a parameter so
-- that it can be built in-place; this also ensures that we only ever
-- generate a result of the appropriate length.
Function Get_Working( Working : Vector:= (2..1 => <>) ) return Vector is
Function "="(Left, Right : String) Return Boolean
renames Ada.Strings.Equal_Case_Insensitive;
Begin
-- We never try to add to a full array.
if Working'Length = Index'Last then
return Working;
end if;
-- We get an integer, or are finished.
loop
Ada.Text_IO.Put( "Enter an integer or type 'Done': " );
READ_INPUT:
Declare
User_Input : Constant String:= Ada.Text_IO.Get_Line;
Begin
return Get_Working( Working & Integer'Value(User_Input) ); --'
Exception
when Constraint_Error =>
if User_Input = "Done" then
return Working;
end if;
End READ_INPUT;
end loop;
End Get_Working;
Begin
Return Get_Working;
End Get_Input;
Procedure Print( Object : Vector ) is
Begin
Ada.Text_IO.Put( '[' );
for Index in Object'Range loop --'
Ada.Text_IO.Put( Integer'Image(Object(Index)) ); --'
if Index /= Object'Last then --'
Ada.Text_IO.Put(',');
end if;
end loop;
Ada.Text_IO.Put( ']' );
End Print;
Begin
declare
V : constant Vector:= Get_Input;
begin
Print( V );
Ada.Text_IO.New_Line( 2 );
end;
Ada.Text_IO.Put_Line( "Done." );
End Example;
Example run:
Enter an integer or type 'Done': 21
Enter an integer or type 'Done': 4
Enter an integer or type 'Done': 18
Enter an integer or type 'Done': DONE
[ 21, 4, 18]
Done.
Upvotes: 1
Reputation: 5021
It appears that your thinking is stuck somewhere between C/C++/Java syntax and Ada. Try the following example.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
type Data_Array is array(Positive range <>) of Integer;
function Collect return Data_Array is
Count : Natural := 0;
begin
Put_Line("Enter the number of data values you will input");
Get(Count);
declare
Nums : Data_Array(1..Count);
begin
Put_Line("Enter" & Count'Image &" values");
for Value of Nums loop
Get(Value);
end loop;
return Nums;
end;
end Collect;
procedure Print(Item : in Data_Array) is
begin
Put_Line("Output of array values:");
for Value of Item loop
Put_Line(Value'Image);
end loop;
end Print;
begin
Print(Collect);
end Main;
The procedure Print takes a parameter of type Data_Array, which is provided by the return value of the function Collect. The function collect only returns an array of exactly the size needed. The procedure Print takes an array of whatever size you send it and prints the contents of that array.
Upvotes: 4