ZetCode

Ada reading input

last modified November 23, 2026

Ada reads from the standard input through the same package that writes to the standard output: Ada.Text_IO. Two styles are available. Line input reads a whole line into a string and leaves the program to interpret it, while formatted input skips whitespace and converts the next item to an integer or a real. This article uses both, checks for the end of the input, and handles the two exceptions that malformed input can raise.

Reading a line

Get_Line reads characters up to the end of the line and stores them in a string. The string is declared with a maximum length, and the second parameter, Last, tells how many characters were actually read. The valid part of the line is then the slice Name (1 .. Last).

read_name.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Read_Name is
   Name : String (1 .. 20);
   Last : Natural;
begin
   Put ("Your name: ");
   Get_Line (Name, Last);
   Put_Line ("Hello there, " & Name (1 .. Last) & "!");
end Read_Name;

Put prints the prompt without a newline, so the typed answer appears on the same line. Get_Line stops at the newline and does not store it. If the line is shorter than 20 characters, the rest of Name keeps its previous contents, which is why only the slice up to Last is used. If the line is longer than 20 characters, the remaining characters stay in the input for the next read.

$ gnatmake -gnat2022 -gnatwa read_name.adb
$ printf 'Ada\n' | ./read_name
Your name: Hello there, Ada!

Reading until the end of the file

A program that reads an unknown number of lines tests End_Of_File before each read. The function has no parameters when it refers to the standard input, and it becomes true when there is nothing left to read. Each call to Get_Line consumes one line, including its newline.

count_lines.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Count_Lines is
   Line  : String (1 .. 80);
   Last  : Natural;
   Count : Natural := 0;
begin
   while not End_Of_File loop
      Get_Line (Line, Last);
      Count := Count + 1;
   end loop;
   Put_Line ("lines =" & Natural'Image (Count));
end Count_Lines;

The loop runs once for each line and stops when the input is exhausted. The content of Line is ignored here; only the fact that a line was read matters. An empty line is still a line: Last is zero, but Count is incremented. The program below is given three lines, so it reports three.

$ gnatmake -gnat2022 -gnatwa count_lines.adb
$ printf 'one\ntwo\nthree\n' | ./count_lines
lines = 3

Reading numbers

Get reads a value rather than a line. It skips spaces and line breaks, then converts the next characters to the item's type. For integers the program instantiates Integer_IO, exactly as it does for output. When no item remains, Get raises End_Error, which is the natural way to end a loop over a stream of numbers.

sum_numbers.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Sum_Numbers is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   use Int_IO;

   N     : Integer;
   Sum   : Integer := 0;
   Count : Natural := 0;
begin
   loop
      begin
         Get (N);
      exception
         when End_Error =>
            exit;
      end;
      Sum := Sum + N;
      Count := Count + 1;
   end loop;
   Put_Line ("count =" & Natural'Image (Count));
   Put_Line ("sum =" & Integer'Image (Sum));
end Sum_Numbers;

The begin ... exception ... end block around Get turns the end of the input into a clean exit from the loop. The sum and the count are kept outside the block, so they survive each iteration. With the three numbers 10, 20, and 30 the program reports a count of 3 and a sum of 60.

$ gnatmake -gnat2022 -gnatwa sum_numbers.adb
$ printf '10 20 30\n' | ./sum_numbers
count = 3
sum = 60

When the input is not a number

Get raises Data_Error when the next item cannot be converted to the requested type. The exception is distinct from End_Error, which means that no item is left at all. The following program reads two integers and catches Data_Error at the level of the whole procedure.

bad_number.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Bad_Number is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   use Int_IO;

   N : Integer;
begin
   Put_Line ("reading two numbers");
   Get (N);
   Put_Line ("first =" & Integer'Image (N));
   Get (N);
   Put_Line ("second =" & Integer'Image (N));
exception
   when Data_Error =>
      Put_Line ("that is not a number");
end Bad_Number;

The input 12 x 30 provides a valid first number and an invalid second item. The first read succeeds, the second raises Data_Error, and the handler prints its message instead of the second value. Because the handler belongs to the procedure body, the program ends after it; a program that must continue would need a handler around a smaller block and a decision about what to do with the bad input.

$ gnatmake -gnat2022 -gnatwa bad_number.adb
$ printf '12 x 30\n' | ./bad_number
reading two numbers
first = 12
that is not a number

Parsing a whole line

When the input is line-oriented, it is often better to read the whole line and convert it with 'Value. This attribute accepts the same syntax as an Ada literal, including a leading sign, and raises Constraint_Error when the text is not a valid value. Leading and trailing spaces are allowed, so a line read with Get_Line can be passed directly.

parse_line.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Parse_Line is
   Line : String (1 .. 40);
   Last : Natural;
begin
   Get_Line (Line, Last);
   Put_Line ("line: " & Line (1 .. Last));
   begin
      Put_Line ("value =" & Integer'Image (Integer'Value (Line (1 .. Last))));
   exception
      when Constraint_Error =>
         Put_Line ("the line is not a number");
   end;
end Parse_Line;

The slice passed to 'Value contains exactly the characters that were read, without the newline. The handler covers only the conversion and the print that depends on it; the first Put_Line has already run, so the bad line is still shown. The same program therefore reports a value for 42 and a message for forty-two.

$ gnatmake -gnat2022 -gnatwa parse_line.adb
$ printf '42\n' | ./parse_line
line: 42
value = 42
$ printf 'forty-two\n' | ./parse_line
line: forty-two
the line is not a number

An applied example: an average

The pieces combine in a program that reads an unknown number of prices and prints their average. Float_IO.Get reads real numbers in decimal notation, and End_Error ends the loop. The result is printed with Float_IO.Put, which the previous article introduced, so the number of decimal places is fixed at two.

average.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Average is
   package Float_IO is new Ada.Text_IO.Float_IO (Float);
   use Float_IO;

   Price : Float;
   Sum   : Float := 0.0;
   Count : Natural := 0;
begin
   loop
      begin
         Get (Price);
      exception
         when End_Error =>
            exit;
      end;
      Sum := Sum + Price;
      Count := Count + 1;
   end loop;

   if Count = 0 then
      Put_Line ("no prices");
   else
      Put ("average = ");
      Put (Sum / Float (Count), Fore => 2, Aft => 2, Exp => 0);
      New_Line;
   end if;
end Average;

The conversion Float (Count) is required because Ada does not mix integer and real arithmetic implicitly. The guard against a count of zero avoids dividing by zero on empty input. For 10.5, 20.0, and 5.5 the sum is 36.0, and the average is 12.00.

$ gnatmake -gnat2022 -gnatwa average.adb
$ printf '10.5 20.0 5.5\n' | ./average
average = 12.00

Next steps

The next article applies the same routines to files. It creates a file, writes lines to it, reads them back, appends to an existing file, and reports a missing file as Name_Error.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have read a line with Get_Line, counted lines until End_Of_File, read integers with Get and ended the loop on End_Error, reported malformed input with Data_Error, converted a whole line with 'Value and Constraint_Error, and computed an average of real numbers. The exceptions article explains the handlers used throughout.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.