comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <laoXhai@ix.netcom.com>
Subject: Re: Converting string to float and integer
Date: 2000/01/09
Date: 2000-01-09T21:32:39+00:00	[thread overview]
Message-ID: <85aupn$q9h$1@nntp2.atl.mindspring.net> (raw)
In-Reply-To: 3vSd4.35559$E36.436449@news2-hme0

In article <3vSd4.35559$E36.436449@news2-hme0>,
	"Daniel Platt" <dan-news@ofdan.co.uk> wrote:

>Hi,
>
>I am tring to convert a string into a float and/or integer.

Here is a little program I sometimes use in Ada classes to 
demonstrate the options for satisfying this requirement.

-- ========== String_To_Scalar_Demonstration ===========
-- String_To_Scalar_Demonstration.adb  by Richard Riehle
-- This program demonstrates several ways to convert a 
-- a string to a scalar value.
-- 
-- =====================================================
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
use  Ada;
procedure String_To_Scalar_Demonstration is
  type Spectrum is (Red, Orange, Yellow, Green,
                    Blue, Indigo, Violet);
  type Unsigned is mod 2**8;
  Num   : Integer  := 0;
  FNum  : Float    := 0.0;
  Color : Spectrum := Blue;
  MNum  : Unsigned := 0;
  Text  : String(1..10);
  Text_Integer  : String := "451";
  Text_Float    : String := "360.0";
  Text_Color    : String := "Orange";
  Text_Unsigned : String := "42";
  Integer_Last  : Natural;
  Float_Last    : Natural;
  Spectrum_Last : Natural;
  Modular_Last  : Natural;
  package SIO is new Text_IO.Enumeration_IO(Enum => Spectrum);
  package MIO is new Text_IO.Modular_IO    (Num  => Unsigned);
  package IIO is new Text_IO.Integer_IO    (Num  => Integer);
  package FIO is new Text_IO.Float_IO      (Num  => Float);
begin
  Text_IO.Put_Line("The String Values are: ");
  Text_IO.Put("Orange for Enumerated Type          ");
  Text_IO.Put_Line("451  for Integer Type  ");
  Text_IO.Put("360.0  for Float Type               ");
  Text_IO.Put_Line("42  for Unsigned Type  ");
  Text_IO.New_Line;
  -- Example 1; using the Value attribute
  Text_IO.New_Line;
  Text_IO.Put_Line("   >>>> Example 1; Using 'Value Attribute <<<< ");
  Color := Spectrum'Value(Text_Color);
  Num   := Integer'Value(Text_Integer);
  FNum  := Float'Value(Text_Float);
  MNum  := Unsigned'Value(Text_Unsigned);
  SIO.Put(Color);   Text_IO.New_Line;
  IIO.Put(Num);     Text_IO.New_Line;
  FIO.Put(Fnum);    Text_IO.New_Line;
  MIO.Put(MNum);    Text_IO.New_Line;
  Text_IO.New_Line;
  -- Example 2; using the procedures of pre-instantiated packages
  Text_IO.Put_Line("   >>>> Example 2; using pre-instantiated packages
<<<< " );
  Integer_Text_IO.Get(From => Text_Integer, 
                      Item => Num, 
                      Last => Integer_Last);
  Float_Text_IO.Get(From => Text_Float,
                    Item => FNum,
                    Last => Float_Last);
  Integer_Text_IO.Put(Num); Text_IO.New_Line;
  Float_Text_IO.Put  (FNum, Fore => 3, Aft => 3, Exp => 0);  
                            Text_IO.New_Line(2);
  -- Example 3; using your own instantiated packages
  Text_IO.Put_Line("   >>>> Example 3; Using own instantiations <<<< ");
  Text_IO.New_Line;
  SIO.Get(From => Text_Color, Item => Color, Last => Spectrum_Last);   
  MIO.Get(From => Text_Unsigned, Item => MNum, Last => Modular_Last);
  IIO.Get(From => Text_Integer, Item => Num, Last => Integer_Last);     
  FIO.Get(From => Text_Float, Item => FNum, Last => Float_Last);
  -- Now Write the Results to the Screen
  SIO.Put(Item => Color);  Text_IO.New_Line;
  IIO.Put(Item => Num);    Text_IO.New_Line;
  FIO.Put(Item => FNum, Fore => 3, Aft => 3, Exp => 0); 
  Text_IO.New_Line;
  MIO.Put(Item => MNum);   
  Text_IO.New_Line(2);
  Text_IO.Put_Line(" **** End of String_To_Scalar_Demonstration **** ");
end String_To_Scalar_Demonstration;
  
Hope this is helpful. You may copy it freely and modify it in any
way you wish. 


Richard Riehle
http://www.adaworks.com




  reply	other threads:[~2000-01-09  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-09  0:00 Converting string to float and integer Daniel Platt
2000-01-09  0:00 ` Richard D Riehle [this message]
2000-01-09  0:00 ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox