From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,eee47022b0e39dbb X-Google-Attributes: gid103376,public From: mfeldman@seas.gwu.edu (Michael Feldman) Subject: Re: Exception problem Date: 1997/02/18 Message-ID: <5eds57$iog@felix.seas.gwu.edu>#1/1 X-Deja-AN: 219751675 References: <330A0D25.313@fs2.assist.uci.edu> Organization: George Washington University Newsgroups: comp.lang.ada Date: 1997-02-18T00:00:00+00:00 List-Id: In article <330A0D25.313@fs2.assist.uci.edu>, Larry Coon 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