Trying to read a number at the console

Paul Dufresne dufrp at hotmail.com
Wed Oct 6 23:56:43 PDT 2004


I appreciate the fact that Brian did not give me what to do, but just a 
hint.

Somehow, I get the feeling that my Console reader flush was ineffective.
It is unclear,  why the first time I was giving something, it was not 
working,
since '34\n' as: Integer gives 34. So if it wasn't stopping on first 2 
digits number,
it would means there was some $\n in the buffer, giving '\n34'. I understand 
that
if next I typed '21', it will give '\n21', as long my Console reader flush 
is
ineffective.

Anyway, I first tried to replace Console reader next: 3 by Console reader 
upToEnd,
thinking that the end, was the end up to now, but it was never ending. So I 
finally
replace it with Console reader upTo: $\n , which seems to work as I wanted.

I probably get the $\n at the beginning, giving '' as: Integer, evaluating 
to 0.
Then, it goes into the whileTrue, trying again, and this time working fine.

Then, what I should ask is why there is a $\n in the buffer in the first 
place.
Which, I still don't know. Not even sure I have supposed correctly what
I was observing.

----Original Message Follows----
From: Brian Rice <water at tunes.org>
Reply-To: Slate project discussion <slate at tunes.org>
To: Slate project discussion <slate at tunes.org>
Subject: Re: Trying to read a number at the console
Date: Wed, 6 Oct 2004 18:38:13 -0700


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/

_________________________________________________________________
Profitez des puissants filtres de courriels indésirables articulés sur la 
technologie brevetée MicrosoftMD SmartScreen. 
http://join.msn.com/?pgmarket=fr-ca&page=features/junkmail Commencez dès 
maintenant à profiter de tous les avantages de MSN Premium et obtenez les 
deux premiers mois GRATUITS*.




More information about the Slate mailing list