comp.lang.ada
 help / color / mirror / Atom feed
* Safely converting String to Float ?
@ 1999-11-03  0:00 Preben Randhol
  1999-11-03  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Preben Randhol @ 1999-11-03  0:00 UTC (permalink / raw)



How do one safely (no program crash) convert a String to a Float?

This little code will dump core:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;

procedure StrToFloat is

   package My_Float_IO is new
     Ada.Text_IO.Float_IO(Float);
   use My_Float_IO;

   Tekst  : String(1..5) := "12.34";
   Tekst2 : String(1..6) := "d12.34";
   Tall  : Float;
   Size  : Integer;
   Tall_F : Float;
   Size_F : Integer;

begin
   Get(Tekst,Tall,Size);
   Get(Tekst2,Tall_F,Size_F);
end;

it will dump core because of the d in Tekst2. Does somebody know how
to do this conversion without core dumping? I'm not trying to convert
the d12.34, but I would like the program to deal with the failure of
conversion in a kinder manner.

I'm using Gnat 3.12p on a Linux 2.2.x System (Redhat 6.0). And I'm an
Ada newbie.

Any hints much appreciated...

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~1999-11-09  0:00 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
1999-11-03  0:00 ` Robert Dewar
1999-11-03  0:00   ` Preben Randhol
1999-11-03  0:00     ` Samuel Tardieu
1999-11-04  0:00       ` Jürgen Pfeifer
1999-11-04  0:00       ` [SOLVED] " Preben Randhol
1999-11-03  0:00 ` Robert Dewar
1999-11-03  0:00 ` Gautier
1999-11-03  0:00 ` Matthew Heaney
1999-11-03  0:00   ` Preben Randhol
1999-11-03  0:00     ` Lutz Donnerhacke
1999-11-03  0:00       ` Preben Randhol
1999-11-08  0:00         ` Robert A Duff
1999-11-09  0:00           ` Preben Randhol

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