Get vs Getline - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

Get vs Getline

Description:

Ada.text_io.get(item= somestring) Requires the user enter 20 characters! Get vs Getline ... ada.text_io.put(item= 'Enter first name ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 6
Provided by: jdch7
Category:
Tags: getline | item

less

Transcript and Presenter's Notes

Title: Get vs Getline


1
Get 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!

2
Get 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

3
Problem
  • 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?

4
String 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

5
With 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
Write a Comment
User Comments (0)
About PowerShow.com