8.1. Input & Output: Communicate with your User - JulTob/Ada GitHub Wiki

Input & Output

Communicate with your User

Text_IO

From the Package Ada.Text_IO we can import many Strings utilities to read data from the user.

with Ada.Text_IO; use Ada.Text_IO;

procedure In_Out is 
  begin
    Skip_Line; --  causes remainder of current input line to be ignored by later calls to get
    end In_Out;


Procedure In IS
   C: Character;
   Is_Read: Boolean;
   S : string(1..50);
   Begin
      Get_Immediate(C);  -- returns the pressed key immediately
      Get_Immediate (C, Is_Read);   -- 'Is_Read' is True if a char was read
      Get_Line(S,Length);
      Stringfunc( S(1..Length) );
      End In;

procedure Out is
   s: String:= "Jon Doe"); c: Character:= 'c';
   begin
      New_Line;
      New_Line(3); -- 3 Lines
      Put("Fixed String");
      Put_Line("Hi");
      Put(s);
      Put_Line(s);
      Put(c);
      End Out;

procedure char_IO is
   c: Character := 'c';
   begin
      Get_Immediate(C); 
      Put(C);
      end char_IO;

Text

with Ada.Text_IO; use Ada.Text_IO;
...
Input: String(1..20);
Last: Natural := 0;
...
Get_Line(Input, Last);

Integers

With Ada.Integer_Text_IO; 

Procedure Read_data is
   use Ada.Integer_Text_IO;
   I: Integer;
   Begin
      Get(I);  
      Get(Item => I);
      Put(I);
      Put(I, 4); -- Width
      End Read_Data;

Or using Ada.Text_IO;

with Ada.Text_IO; use Ada.Text_IO;
...
Input: String(1..9);
Last: Natural := 0;
Val: Integer := 0;
...
Get_Line(Input, Last);
Val := Integer'Value(Input(1..Last));

Floats

with Ada.Float_Text_IO;
use Ada.Float_Text_IO; 

procedure Float_IO is
  f: float
  begin
    Get( F );
    Put( F );
    Put( F, Aft => 1, Exp => 0);
    End Float_IO;

 

Example

Function Get_Input return Integer is
   Input: String := "            ";
   EOL: constant Character 
      := Character'Val(13); -- End of Line
   I: Integer:= 1;
   C: Character:= ' ';
   Is_Read: Boolean;
   Begin
      Loop
         Get_Immediate (C, Is_Read); -- reads 1 char
         Exit when C = EOL;
         If Is_Read Then   -- True if a char was read
            Put(C); -- Prints C without jumping line
            Input(I) := C;
            I:= I + 1;
            End If;
          End Loop;
          return Integer'Value(Inp(1..I));
      End GetAns;
With Text_IO;
Use Text_IO;

Procedure Output is
  c: Character;
  s: String := "Hello World";
  Begin
    New_Line;
    Put(s);
    Put_Line(s);
    Put_Line("Bye");
    Put(c);
    Put_Line(c);
    New_Line(3);  -- Three new lines.
    End Output;
With Ada.Integer_Text_IO; USE Ada.Integer_Text_IO;

procedure Output is
  n: Integer;
begin
  Put(n);
  Put(n, 3); -- Tabulated to fit in three spaces
  Put(n, Width => 3);

  end Output;
with Ada.Float_Text_IO;

procedure Output is
 use Ada.Float_text_IO;
   f: Float := 0.5;
   s_f: string := Float'Image(f)
   n_f_rounded : integer := Integer(f)
begin
   Put(f);  -- Scientific notation
   Put(f, Exp => 0);  -- Normal

   end Output;

Enumerations

Type Students is (Alice, Bob, Charlie, Dani);
package names_IO is new Ada.Text_IO.Enumeration_IO(students); 
use names_io;

Booleans

Boolean'Image should provide, but there's a way to actualize the package of enumeration_IO for Boolean.

package BIO is new Text_IO.Enumeration_IO(Enum => Boolean); 
B: Boolean := False;
...
BIO.Put(B);