Title: Get vs Getline
1Get vs Getline
- Using Ada.text_io.get requires that the user
enter exactly the number of characters specified. - For example
- Somestring string(1..20)
- Begin
- Ada.text_io.get(itemgtsomestring)
- Requires the user enter 20 characters!
2Get vs Getline
- An alternative is to use Ada.text_io.get_line
- For example
- Subtype str_lngth_type is integer range 0..20
- Str_lngth str_lngth_type
- Somestring string(1..20)
- Begin
- Ada.text_io.get_line(itemgtsomestring,
lastgtstr_lngth) - This will read characters into the string until
EOL
3Problem
- If the user only enters 5 characters and then
presses return, what is in the other 15
characters? - Subtype str_lngth_type is integer range 0..20
- Str_lngth str_lngth_type
- Somestring string(1..20)
- Begin
- Ada.text_io.get_line(itemgtsomestring,
lastgtstr_lngth) - Ada.text_io.put(itemgtsomestring)
- What does this output?
4String Slicing
- String slicing fixes this problem
- Subtype str_lngth_type is integer range 0..20
- Str_lngth str_lngth_type
- Somestring string(1..20)
- Begin
- Ada.text_io.get_line(itemgtsomestring,
lastgtstr_lngth) - Ada.text_io.put(itemgtsomestring(1..str_lngth))
- Only print the characters that were entered
5With ada.text_io Procedure sample
IS Max_Name_length constant integer
20 Subtype name_length_type is integer range
0..Max_Name_length First_name_length
name_length_type Last_name_length
name_length_type First_name string(1..max_name_
length) Last_name string(1..max_name_length)
Begin --sample ada.text_io.put(itemgtEnter
first namegt ) ada.text_io.get_line(itemgtfirst_
name, lastgt first_name_length) ada.text_io.put(
itemgt Enter last namegt ) ada.text_io.get_line
(itemgtlast_name, lastgt last_name_length) ada.t
ext_io.put(itemgt first_name(1..first_name_length)
) ada.text_io.put(itemgt ) ada.text_io.put(
itemgt last_name(1..last_name_length) ada.text_i
o.new_line End sample