

On Sat, 12 Oct 2002 Fergus Henderson wrote :
>On 12-Oct-2002, Noel  Pinto <cool4life@rediffmail.com> wrote:
> > Below is code which squares a particular number 12. I want 
>that
> > number to be entered by the user. But I do not know how to 
>accept
> > integers.
>
>Generally the best way to accept integers from the user is to 
>read a
>line of text using io__read_line_as_string, and then convert the 
>string
>to an integer using string__to_int (for `int', the 
>limited-range
>integer type) or integer__from_string (for `integer', the 
>unbounded
>integer type).
>
>Here's an example.
>
>:- module read_int.
>:- interface.
>:- import_module io.
>
>:- pred main(io__state::di, io__state::uo) is det.
>
>:- implementation.
>:- import_module string, exception.
>
>main -->
> 	print("Enter an int: "), flush_output,
> 	read_int(Int),
> 	print("You entered: "), print(Int), nl.

What is the purpose of puttinf 'flush_output'? The code works fine 
without it, So what's the need?


>:- pred read_int(int::out, io__state::di, io__state::uo) is 
>det.
>read_int(Int) -->
> 	io__read_line_as_string(Result),
> 	{
> 		Result = ok(Line),

Does 'ok(Line)' mean it is checking whether the line is empty? Is 
'Line' a variable?

> 		remove_suffix(Line, "\n", IntString),
> 		string__to_int(IntString, IntVal)
> 	->
> 		Int = IntVal
> 	;
> 		% better error handling is left as an exercise for the
> 		% reader...
> 		throw("invalid entry, end-of-file, or I/O error")
> 	}.


--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users@cs.mu.oz.au
administrative address: owner-mercury-users@cs.mu.oz.au
unsubscribe: Address: mercury-users-request@cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request@cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------

