Trying to read a number at the console

Brian Rice water at tunes.org
Wed Oct 6 18:38:13 PDT 2004


On Oct 6, 2004, at 3:30 PM, Paul Dufresne wrote:

> I am trying the following code to read a number at the Console:
> ===
> prototypes addPrototype: #GuessNumber derivedFrom: {Cloneable}
> ...
> gn@(GuessNumber traits) readNumber
> "return a number between 1 and 100 read at the Console
> if the number is unreadable (=0) then ask again "
> [ | num |
>  Console reader flush.
>  num: ((Console reader next: 3) as: Integer).
>  [num isZero]
>    whileTrue:
>      [
>      Console reader flush.
>      num: ((Console reader next: 3) as: Integer).
>      ].
>  Console reader flush.
>  num
> ].
> ===
> But it is not working as expected.
> Entering only 2 digits numbers, followed by return, seems to loop 
> forever.
> It seems to stop after entering something after having entered a 3 
> digits number.
> I expect that if I enter a 2 digit number it simply returns it.
> And entering text, should loop as long as a number is not given.
>
> Someone have a clue on what is wrong?

A hint: #next: is unaware of carriage-returns / line-endings. It just 
grabs the next 3 elements from the stream.

Although the idea that non-numeral strings return 0 on conversions to 
integers bothers me. I cannot help but think that an error should be 
raised instead.

--
Brian T. Rice
LOGOS Research and Development
http://tunes.org/~water/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1383 bytes
Desc: not available
Url : /archives/slate/attachments/20041006/0d902921/attachment.bin


More information about the Slate mailing list