Input of Strings and Characters, EOL and EOF


Output of Strings

         with Ada.Text_IO; use Ada.Text_IO;

         procedure io is
         begin
    
            put("Hello");
            put_line("There");
            put_line("Hi Mom!");
            new_line(2);

            put_line("ITEC 320" & " is FUN" & "!");

            set_col(3);
            put_line("I like Ada!");
        
         end io;
    OUTPUT:
    HelloThere
    Hi Mom!


    ITEC 320 is FUN!
      I like Ada!
  

Input a String

        -- Demonstrates input of a string

        with Ada.Text_IO; use Ada.Text_IO;
        procedure doStrings1 is
           s: String(1 .. 2);           
           t: String(1 .. 3);          
        begin
           put("Enter 5 characters and press return: ");
           -- input exactly 2 characters
           get(s);
           put_line("first 2: " & s);

           -- input exactly 3 characters
           get(t);
           put_line("next 3: " & t);
           new_line;

           put("Enter 5 characters on several lines: ");

           -- input exactly 2 characters, spread over several lines
           get(s);
           put_line("first 2: " & s);

           -- input exactly 3 characters, spread over several lines
           get(t);
           put_line("next 3: " & t);
        end doStrings1;

        SAMPLE RUN:
        Enter 5 characters and press return: abc d
        first 2: ab
        next 3: c d

        Enter 5 characters on several lines:a
        b
        first 2: ab
        c
        d
        e
        next 3: cde
  

Get_Line


More on Get_Line

 
        with Ada.Text_IO; use Ada.Text_IO;
        procedure doStrings4 is
           s: String(1 .. 4);           
           len: Natural;

        begin
           for i in 1 .. 6 loop
              get_line(s, len);
              put_line(s(1 .. len));  -- Slice

              if len = 4 then
                 skip_line;
              end if;

           end loop;
        end doStrings4;

SAMPLE RUN:
abcdefg
abcd
pqrstuv
pqrs
1234
1234
mno
mno
abcd
abcd
abcde
abcd

End_Of_File

  1. How to test for end of file: Ada.Text_IO.End_Of_File

  2. Example - sum up an unknown number of integers:

 
        -- Illustrates use of End_Of_File and integer get
        with Ada.Text_IO; use Ada.Text_IO;
        with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

        procedure eof4 is
           i: Integer;
           s: Integer := 0;

        begin
           while (not End_Of_File) loop
              get(i);
              s := s + i;
           end loop;

           put(s);
           new_line;
        end eof4;
  

Application of End_Of_File - Echo input

        -- Echoes input.
        -- Reads and writes one character at a time
        with Ada.Text_IO; use Ada.Text_IO;

        procedure eofloop3a is
           c: Character;
        begin
           while (not End_Of_File) loop
              get(c);
              put(c);
           end loop;
        end eofloop3b;
  

Echo input - Improved version

        -- Echoes input.
        -- Reads and writes one character at a time
        with Ada.Text_IO; use Ada.Text_IO;

        procedure eofloop3b is
           c: Character;
        begin
           while (not End_Of_File) loop

              get(c);
              put(c);

              -- Move output to new line as needed
              if End_Of_Line then
                  new_line;          
              end if;

           end loop;
        end eofloop3b;
  

Get_Immediate


Look_Ahead


End_Of_File - Echo input using get_line

        -- Echoes its input.
        -- Demonstrates using multiple get_lines to input lines that are
        -- longer than 4 characters 
        with Ada.Text_IO; use Ada.Text_IO;
        procedure eofLoop2 is
           maxLength: Positive := 4;
           s: String(1 .. maxLength);           
           length: Natural;

        begin
           while (not End_Of_File) loop

              get_line(s, length);
              put(s(1 .. length));

              if length < maxLength then
                 new_line;
              elsif length = maxLength and End_Of_Line then
                 skip_line;
                 new_line;
              end if;

           end loop;
        end eofLoop2;

SAMPLE RUN: 
abc
abc
abcdef
abcdef
ghijklkm
ghijklkm
a
a
^D
  

Running Programs Interactively vs Using Redirection


Skip_Line

        -- Demonstrates how skip_line moves to next line, 
        -- and ignores the rest of a line

        with Ada.Text_IO; use Ada.Text_IO;
        procedure doStrings1 is
           s: String(1 .. 2);
           t: String(1 .. 3);

        begin
           put("Enter 2 or more characters and press return: ");
           get(s);
           put_line("first 2: " & s);
           new_line;

           skip_line;

           put("Enter 3 or more characters and press return: ");
           get(t);
           put_line("next 3: " & t);
        end doStrings1;
SAMPLE RUN:
Enter 2 or more characters and press return: abcde
first 2: ab

Enter 3 or more characters and press return: PQRST
next 3: PQR
  

Character, Get/Put Loop, End_Of_Line

        -- Demonstrates how end_of_line tests for end of line
        -- and how skip_line moves to next line
        with Ada.Text_IO; use Ada.Text_IO;
        procedure doStrings2 is
           c: Character;
           s: String(1 .. 80);           
           length: Natural;

        begin
           put("Enter some text and press enter: ");
           while (not Ada.Text_IO.End_Of_Line) loop
              get(c);
              put(c);
           end loop;

           skip_line;  -- move to next line of input
                       --   since we use get_line next

           new_line;

           put("Enter another line: ");

           get_line(s, length);

           put_Line(s);
        end doStrings2;

SAMPLE RUN:
Enter some text and press enter: ab
ab
Enter another line: def
def
  

Summary of input routines

 
   i: integer;
   c: character;
   s: String(1..80);

An important WARNING on input from a file


A final word on EOF in interactive programs