3.2 Using the numberio Fragment

A program using the numberio fragment will have the following structure:

INCLUDE '~beta/basiclib/numberio
--- program: descriptor ---
(# ...
   r: @real;
do ...
   getReal -> r;
   ...
#)

3.2.1 Reading numbers from standard input

The following example illustrates using numberio to read general numerics, as well as based, integer and reals from keyboard.

Program 2: NumberioDemo.bet

ORIGIN '~beta/basiclib/numberio';
--- program: descriptor ---
(#
do L: (#
      do 'Enter a general number: '->puttext;
         getNumber
         (# valueError::< (# do msg[]->screen.putline; true->continue #);
            syntaxError::< (# do msg[]->screen.putline; get; true->continue #);
            baseError::< (# do msg[]->screen.putline; true->continue #);
            integerValue::<
              (# do '\tInteger='->puttext; value->putInt #);
            basedValue::<
              (#
              do '\tBase='->puttext; base->putint;
                 ' Value='->puttext; value->putInt;
                 ' BasedValue='->puttext; (base,value)->putBased;
              #);
            realValue::<
              (# 
              do '\tReal='->puttext; 
                 value->screen.putReal(# format::< (# do exp->style; true->upcase #)
              #) #);
         do newline;
         #);
         'Enter a based number: '->puttext;
         getBased
         (# valueError::< (# do msg[]->screen.putline; true->continue #);
            syntaxError::< (# do msg[]->screen.putline; true->continue #);
            baseError::< (# do msg[]->screen.putline; true->continue #);
         do '\tBase='->puttext; b->putint;
            ' Value='->puttext; i->putInt;
            ' BasedValue='->puttext; (b,i)->putBased;
         #); newline;
         'Enter a real number: '->puttext;
         getReal
         (# valueError::< (# do msg[]->screen.putline; true->continue #);
            syntaxError::< (# do msg[]->screen.putline; true->continue #);
            baseError::< (# do msg[]->screen.putline; true->continue #);
         do '\tReal='->puttext
         #)->screen.putreal(# format::< (# do exp->style; true->upcase #) #); newline;
         'Enter an integer number: '->puttext;
         getInteger
         (# valueError::< (# do msg[]->screen.putline; true->continue #);
            syntaxError::< (# do msg[]->screen.putline; true->continue #);
            baseError::< (# do msg[]->screen.putline; true->continue #);
         do '\tInteger='->puttext
         #)->putInt; newline;
         restart L
      #)   
#)


Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Monday October 23rd 2000 at 11:49]