From: "Steve Doiel" <nospam_steved94@attbi.com>
Subject: Re: Incompatible type error handling
Date: Tue, 11 Dec 2001 03:19:20 GMT
Date: 2001-12-11T03:19:20+00:00 [thread overview]
Message-ID: <YWeR7.5788$7y.24155@rwcrnsc54> (raw)
In-Reply-To: bf8dbe70.0112101352.20a3d257@posting.google.com
Here is a small sample program that illustrates some crude error handling,
but you may find some suprises:
with Ada.Integer_Text_Io;
with Ada.Float_Text_io;
with Ada.Text_io;
procedure Input_Demo is
integer_value : Integer;
float_value : Float;
begin
loop
begin
Ada.Text_Io.Put( "Enter an Integer > " );
Ada.Integer_Text_Io.Get( integer_value );
Ada.Text_Io.Skip_Line;
exit;
exception
when others =>
Ada.Text_Io.Put_Line( "That's not a valid integer" );
Ada.Text_Io.Skip_Line;
end;
end loop;
Ada.Text_Io.Put( "You entered " );
Ada.Integer_Text_Io.Put( integer_value );
Ada.Text_Io.New_Line;
loop
begin
Ada.Text_Io.Put( "Enter a floating point value > " );
Ada.Float_Text_Io.Get( float_value );
Ada.Text_Io.Skip_Line;
exit;
exception
when others =>
Ada.Text_Io.Put_Line( "That's not a valid floating point value" );
Ada.Text_Io.Skip_Line;
end;
end loop;
Ada.Text_Io.Put( "You entered " );
Ada.Float_Text_Io.Put( float_value );
Ada.Text_Io.New_Line;
end Input_Demo;
--------
Try a few of the following cases in response to each of the prompts:
abc
123
123.45
If you more control over what is accepted, I suggest you read the input into
a string, validate the input string, and then convert to the appropriate
type.
SteveD
"Ben" <cptnben@gisco.net> wrote in message
news:bf8dbe70.0112101352.20a3d257@posting.google.com...
> Hi,
>
> I am new to Ada and the instruction book I found is not really helpful
> in explaining how to handle errors of unmatching types. For example,
> one of the sample problems the book has me do it a simple calculator
> made to work with real values. Say I programmed it to work only with
> integers, yet I enter a real number. That will cause a problem,
> right? Is the error handling automatic in Ada, or do I need to code
> something in. So far I have been unsuccessful at finding example code
> and instructions on how to do this.
>
> Thanks,
> Ben
prev parent reply other threads:[~2001-12-11 3:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-12-10 21:52 Incompatible type error handling Ben
2001-12-10 22:24 ` Mark Johnson
2001-12-10 23:03 ` Mark Lundquist
2001-12-11 3:19 ` Steve Doiel [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox