comp.lang.ada
 help / color / mirror / Atom feed
* Incompatible type error handling
@ 2001-12-10 21:52 Ben
  2001-12-10 22:24 ` Mark Johnson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ben @ 2001-12-10 21:52 UTC (permalink / raw)


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



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

* Re: Incompatible type error handling
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Johnson @ 2001-12-10 22:24 UTC (permalink / raw)


Ben wrote:
> 
> 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?  
Hmm. It depends on what you do and how you do it. Let's try a few
examples...
 - Integer_IO.Get - skips leading white space, reads a + or - sign, then
basically reads digits. If a decimal point is provided, it stops reading
at the decimal point. Exception Data_Error generated if the value is
illegal (various causes). 
Your code at this point will likely fail on reading the rest of the line
of text (finding a period instead of an operator).
 - Integer'Value - similar sequence, but raises Constraint_Error in case
of error.
What really happens depends on the code you read the string with.
 - read a real & assign to an integer
You get a compile error (if conversion omitted) or the results of the
explicit real to integer conversion (round).
 - read one character at a time & interpret.
It would do whatever you coded it to do.

As with anything I have to deal with on a computer - I suggest using the
correct data types for all cases....

> 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.
Not quite sure what you mean by "automatic". If you expect the run time
to catch an exception and ask you to type the value in again - sorry -
you have to do that yourself. Something like a begin / end with
exception handler like....
  begin
  (read the stuff here)
  exception
    when E : others =>
      Put_Line("Can't understand input " && [their input here]);
      Put_Line(Exception_Message(E));
  end;
within a loop that keeps asking until you get the input correct.

Of course, for a homework assignment, you might be tempted to just
assume valid input. That will make your code about 1/3rd to 1/2 the size
a robust version would be.
  --Mark



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

* Re: Incompatible type error handling
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Lundquist @ 2001-12-10 23:03 UTC (permalink / raw)



"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?

Probably so... at least, the calculator is not going to do what the user has
in mind!

Actually, this issue has nothing to do with "types" or type mismatch.  You
are not really entering any kind of a number, you're entering a string.  The
process of getting some kind of number from that is not one of "conversion"
(as beginners often term it), but one of interpretation.  Ada defines some
elementary facilities that you can build on to do whatever interpretations
you need; but these facilities necessarily only know about one type, and
that is the type you are asking for.  So if you type "123.456" into your
calculator, Ada is not going to say "that's not an Integer, it's a Real
Number", any more than if you typed "cow" it would say "that's not an
Integer, it's an Animal".  I mean, "12.10" could be real number, or it could
be today's date, right?

In your integer calculator, "3.14159" is not a real number, it is nonsense,
and you'll have to program your calculator to recognize whatever you define
to be nonsense and handle that nonsense in whatever way you think a
calculator should handle it.

> Is the error handling automatic in Ada,

There's no error as far as Ada is concerned, only as far as your calculator
is concerned.

Hope this helps,
-- mark






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

* Re: Incompatible type error handling
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Doiel @ 2001-12-11  3:19 UTC (permalink / raw)


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





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

end of thread, other threads:[~2001-12-11  3:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox