Re: [eclipse-clp-users] Read integer from user (without a dot)

From: Joachim Schimpf <jschimpf_at_coninfer.com>
Date: Wed, 21 Sep 2011 21:14:48 +0200
Kish Shen wrote:
> On 21/09/2011 18:22, Sergey Dymchenko wrote:
>> Sorry for probably stupid question, but I can't find an answer.
>>
>> How to read an integer from user?
>> read(N) requires user to end input with dot,
>> read_line(N) returns a string, not an integer...
>>
>> Sergey.
>>
 >
> Hi,
> 
> If you use ECLiPSe's help/1 with the query "help read.", this would give 
> you a list of all builtins whose name has read in it (a predicate that 
> provides the functionality you want is likely to have read as part of 
> its name), including
> 
> read_token/2 and read_token/3, which is probably what you want - call it 
> with Class set to integer:
> 
> read_token(stream, X, integer)
> 


In Sergey's case, a better choice might be the combination

     read_string(Stream, end_of_line, _, String),
     number_string(X, String),
     integer(X)

because (unlike read_token) it handles negative numbers and
it will fail if there are any junk characters after the number
(read_token would just leave these unread).


-- Joachim
Received on Wed Sep 21 2011 - 19:14:57 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET