[Beginner] A question regarding reading from keyboard

Hello, I am a beginner at Ada and programming in general. Currently,
I am practising by writing programs that perform operations on text I input via
the keyboard.
I have several questions about the best practices for doing so.

The code below is my learning attempt for different methods for reading:

with Ada.Text_IO; use Ada.Text_IO;
--This program will echo what the user types in.
procedure Main is
   input : Character;
   line_end : Boolean := False;

   Put_Line("Enter some characters.");
   Put_Line("Output is character by character.");
      exit when End_Of_Line;
   end loop;

   Put_Line("This uses strings.");
   Put_Line("Enter a sentence: ");
   Look_Ahead (Item        => input,
               End_Of_Line => line_end);
   if line_end then
      Put_Line ("Arr, end of the line matey");
      Put (input);
   end if;
      s : String := Get_Line;
end Main;

I noticed that the declaration for the String ‘s’ would read a new line and end the program without myself being able to type any characters.

Question 1: Which part of the code is resulting in a new line being left to read for the Get_Line function? If I add the Skip_Line procedure just before the new line check, I dont have any issues.

Question 2: What is the best practice for reading in things from the keyboard?

I appreciate any hints. Thank you.

Keyboard input tends to be buffered, which results in behavior that differs from what might be expected. The general rule is to always read complete lines using the Get_Line function, and then parse the resulting String. That avoids any surprises.


Get consumes a character from the input buffer, but not the end-of-line character(s). When Get_Line runs, it consumes the end-of-line character(s) and return an empty string. That’s why Skip_Line solves the issue.

1 Like