Personally, I find that it's easier to use a package that defines Input-Output operations directly for an Unbounded_String. It's easy to define such a package, so I've provided one for you.
I call my package ``Ustrings'', which is a nice short name - I'll explain later why it has that name. It has a procedure Get_Line, which reads in a whole text line into an Unbounded_String. Procedure Put prints the Unbounded_String. Procedure Put_Line first Puts the Unbounded_String, and then starts a new line. Here's a shortened version of this package's declaration:
package Ustrings is procedure Get_Line(Item : out Unbounded_String); procedure Put(Item : in Unbounded_String); procedure Put_Line(Item : in Unbounded_String); end Ustrings;
If you're curious you can see the complete declaration (specification) and body of my package Ustrings.
Also, I also believe that ``Unbounded_String'' is too long a name for such a widely-used type, so I define in package ``Ustrings'' a new name for Unbounded_String called ``Ustring''. You can declare variables of type ``Ustring'' and they'll simply be Unbounded_Strings. You do not need to use the type name "Ustring" instead of "Unbounded_String"; I simply find it convenient.
Let's look at a short Unbounded_String demonstration program named `Unbound'. It reads in text, one line at a time, and then does various things with the line of text. Study the following program and see if you can figure out what it does.
with Ada.Strings.Unbounded, Ustrings, Text_IO, Ada.Integer_Text_IO; use Ada.Strings.Unbounded, Ustrings, Text_IO, Ada.Integer_Text_IO; procedure Unbound is -- Demonstrate Unbounded_String. Input : Unbounded_String; Stop : constant Unbounded_String := To_Unbounded_String("stop"); begin Put_Line("Please type 'stop' to end this program."); loop New_Line; Put_Line("Please type in a line:"); Get_Line(Input); exit when (Input = Stop); Put("You just typed in:"); Put_Line(Input); Put("This input line contains "); Put(Length(Input)); Put_Line(" characters."); for I in reverse 1 .. Length(Input) loop Put(Element(Input, I)); end loop; New_Line; end loop; end Unbound;
What does subprogram Unbound do after it prints the line showing the number of characters in the input string?
|Go back to the previous section||Skip to the next section||Go up to lesson 8 outline|
David A. Wheeler (firstname.lastname@example.org)
The master copy of this file is at
The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s8sf.htm".