* Re: String Segment to Integer Question
1997-09-28 0:00 String Segment to Integer Question David Conly
@ 1997-09-29 0:00 ` Richard A. O'Keefe
0 siblings, 0 replies; 2+ messages in thread
From: Richard A. O'Keefe @ 1997-09-29 0:00 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1606 bytes --]
David Conly <conly@cs.und.edu> writes:
>Is there any way to take a portion of a string inputed by the user at
>the keyboard and make it into an integer.
The keyboard has nothing to do with it.
with Ada.Text_IO, Ada.Integer_Text_IO;
procedure Demo is
procedure Use_Value(S: in String; H: out Integer; M: out Integer) is
-- Precondition:
-- S = "H" & "h" & "MN" for some digits H, M, N.
-- Postcondition:
-- H = "H" as an integer
-- M = "MM" as an integer
-- Method:
-- Uses standard <integer type>'Value attribute.
begin
H := Integer'Value(S(1..1));
M := Integer'Value(S(3..4));
end Use_Value;
procedure Use_Get(S: in String; H: out Integer; M: out Integer) is
-- Interface:
-- same as Use_Value
-- Method:
-- Uses Ada.Integer_Text_IO.Get from a string.
Last: Positive;
begin
Ada.Integer_Text_IO.Get(S(1..1), H, Last);
Ada.Integer_Text_IO.Get(S(3..4), M, Last);
end Use_Get;
procedure Show_Result(S: in String; H, M: in Integer) is
begin
Ada.Text_IO.Put('(');
Ada.Integer_Text_IO.Put(H, Width => 1);
Ada.Text_IO.Put(',');
Ada.Integer_Text_IO.Put(M, Width => 2);
Ada.Text_IO.Put(") <= '");
Ada.Text_IO.Put(S);
Ada.Text_IO.Put_Line("'.");
end;
S: constant String := "8h32";
H, M: Integer;
begin
Use_Value(S, H, M);
Show_Result(S, H, M);
Use_Get(S, H, M);
Show_Result(S, H, M);
end Demo;
--
John �neas Byron O'Keefe; 1921/02/04-1997-09-27; TLG,TLTA,BBTNOTL.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/%7Eok; RMIT Comp.Sci.
^ permalink raw reply [flat|nested] 2+ messages in thread