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!
get(s)
always tries to read s'length
characters
get(s)
ignores newlines!
-- 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
Ada.Text_IO.Get_Line(s, len)
len
will contain the number of characters that
were read s(1..len)
will contain the characterswith Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure doStrings2 is s: String(1 .. 80); len: Natural; begin put("Enter fewer than 80 characters: "); get_line(s, len); put("len = "); put(len, 0); new_line; put_Line("You entered |" & s(1 .. len) & "|"); put_Line("All of s: |" & s & "|"); end doStrings2; SAMPLE RUN: Enter fewer than 80 characters: cde len = 3 You entered |cde| All of s |cde$ðÿ¾õ `¨|
Get_line(s, l)
fills s
until it reaches the end of
either s
or the line.
s
have their previous
contents!!
s(1 .. len)
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
Ada.Text_IO.End_Of_File
-- 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;
-- 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;
-- 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;
-- Echoes input, using get_immediate. with Ada.Text_IO; use Ada.Text_IO; procedure eofloop4 is c: Character; begin loop get_immediate(c); put(c); exit when c = 'x'; end loop; end eofloop4;
Ada.Text_IO.look_ahead
allows the user to view the next character in the input without
consuming that character (ie without moving the input
pointer)with Ada.Characters.Handling; use Ada.Characters.Handling; with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure lookahead is c: Character; i: Integer; line: String(1 .. 80); len: Natural; EOL: Boolean; begin put("Enter a number or a string: "); look_ahead(c, EOL); put_line("The first character in the input: " & c); put_line("At end of line: " & Boolean'Image(EOL)); new_line; if Ada.Characters.Handling.is_digit(c) then get(i); put("You entered this number: "); put(i); new_line; else get_line(line, len); put("You entered this string: "); put_line(line(1 .. len)); end if; end lookahead; SAMPLE RUN 1 Enter a number or a string: 123 The first character in the input: 1 At end of line: FALSE You entered this number: 123 SAMPLE RUN 2 Enter a number or a string: abcde The first character in the input: a At end of line: FALSE You entered this string: abcde SAMPLE RUN 3 Enter a number or a string: The first character in the input: At end of line: TRUE EOL is true. You must have just hit enter
-- 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
>eofloop3 line line another line another line ^D
^D
on a line by itself
will cause end of file to return true. Enter ^D
by holding down control and D at the same time.^Z <Enter>
on a line by itself
will cause end of file to return true. Enter this
by holding down control and Z at the same time,
and then pressing the Enter/Return key.>cat foo line 1 line 2 >eofloop3 < foo line 1 line 2
>eofloop3 < foo > goo >cat goo line 1 line 2
>cat moo line A line B >eofloop3 < moo >! goo >cat goo line A line B
Skip_Line
causes remainder of current input line
to be ignored by later calls to get-- 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
-- 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
i: integer; c: character; s: String(1..80);
If the line has fewer than 80 characters, then the input marker moves to the next line and the next input will read from that next line. If the line has 80 or more characters, then the input marker remains on the current line.