From: mfeldman@seas.gwu.edu (Michael Feldman)
Subject: Re: Exception problem
Date: 1997/02/18
Date: 1997-02-18T00:00:00+00:00 [thread overview]
Message-ID: <5eds57$iog@felix.seas.gwu.edu> (raw)
In-Reply-To: 330A0D25.313@fs2.assist.uci.edu
In article <330A0D25.313@fs2.assist.uci.edu>,
Larry Coon <larry@fs2.assist.uci.edu> wrote:
> loop
> begin
> Put ("Enter a positive number: ");
> Get (X);
> exit; -- No exception, so input was valid.
> exception
> when Constraint_Error =>
> Put_Line ("Entry must be positive. Try again.");
> when Data_Error =>
> Put_Line ("Entry must be a number. Try again.");
You need
Skip_line;
here, which advances the input beyond the line terminator.
The problem is that if your first character is non-numeric,
the exception is read and the character is left in the buffer.
This is correct RM behavior; numeric input ceases when a character
is encountered that can't be part of a numeric token. It stays in
the input buffer because it might have been intended to be picked
up on the next read.
> end;
> end loop;
>The idea is that if input is not valid an exception will be thrown, the
in Ada jargon, exceptions are "raised" and "handled".
>appropriate message will be displayed, and the code will loop back to
>the prompt. If no exception is thrown the input is valid, and the exit
>statement take the program out of the loop.
Right - a standard Ada "robust input loop".
>This code handles non-positive numeric input (eg: 0 or -3) correctly.
>But when I give it non-numeric input (eg: C), it displays the "Entry
>must be a number. Try again" message repeatedly and never stops for
>input again.
Well, because that character is left in the buffer and never read,
your program loops and keeps trying to read it again and again.
This is a very common situation for beginners. I think it's discussed
in the programmer FAQ accessible from www.adahome.com. It's also
covered in some detail in that textbook by Feldman/Koffman, in Chap. 6.
Since it's quite proper RM behavior, you'll find this to be compiler-
independent.
I hope this helps.
Mike Feldman
next prev parent reply other threads:[~1997-02-18 0:00 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-02-18 0:00 Exception problem Larry Coon
1997-02-18 0:00 ` Michael Feldman [this message]
1997-02-19 0:00 ` Larry Coon
1997-02-19 0:00 ` Joerg Rodemann
1997-02-19 0:00 ` Mats Weber
1997-02-19 0:00 ` David C. Hoos, Sr.
1997-02-19 0:00 ` Larry Coon
1997-02-19 0:00 ` Robert Dewar
1997-02-19 0:00 ` Keith Allan Shillington
1997-02-22 0:00 ` Arthur Evans Jr
1997-02-24 0:00 ` Larry Coon
1997-02-24 0:00 ` Larry Kilgallen
1997-02-24 0:00 ` Larry Coon
1997-02-25 0:00 ` Fergus Henderson
1997-02-25 0:00 ` Do-While Jones
1997-03-09 0:00 ` John Volan
1997-03-09 0:00 ` Robert Dewar
1997-03-12 0:00 ` Keith Thompson
1997-02-25 0:00 ` Robert I. Eachus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox