comp.lang.ada
 help / color / mirror / Atom feed
From: gentle@cnj.digex.net (Gentle)
Subject: Re: How to convert string 2 int...
Date: 27 Sep 1994 12:00:30 GMT
Date: 1994-09-27T12:00:30+00:00	[thread overview]
Message-ID: <3691gu$kp4@cnj.digex.net> (raw)
In-Reply-To: CwqqL5.CEv@umassd.edu

Paul Baptista (bub@cis.umassd.edu) wrote:
: I'm starting to learn ADA.  I know with other languages we can convert
: a character variable to an integer.  I was wonder can ADA do this?
: I'm trying to write a program that accepts a string from the user.
: Then it will parse out all the numbers in the string and print the total.

: bub@cis.umassd.edu

  That's easy!  There's an Ada call in TEXT_IO for integers (referencing
the generic package INTEGER_IO):
  GET (FROM : in STRING; ITEM: out NUM; LAST : out POSITIVE);

Try this:

with TEXT_IO;
use  TEXT_IO;

procedure BLAH is
  subtype MYNUM is INTEGER range 0 .. 1000;
  package MYNUM_IO is new INTEGER_IO (MYNUM);

  subtype LINE_RANGE is INTEGER range 1 .. 80;

  USER_INPUT : STRING (LINE_RANGE);
  USER_NUM   : MYNUM := 0;

  NULL_INPUT : exception;

  LLAST      : INTEGER;  -- MUST be integer for GET_LINE call.
  STRIX      : INTEGER;  -- MUST be same type as LLAST
begin
  -- Get input from user
  GET_LINE (USER_INPUT, LLAST);

  -- Check for input error
  if LLAST = 0 then
    raise NULL_INPUT;
  end if;

  -- Initialize string index for parsing
  STRIX := LINE_RANGE'FIRST;
  while STRIX <= LINE_RANGE'LAST loop
    MYNUM_IO.GET (
      FROM => USER_INPUT (STRIX .. LINE_RANGE'LAST),
      ITEM => USER_NUM,
      LAST => LLAST);

    -- LLAST is returned such that USER_INPUT(LLAST) was the last
    -- character read from the string.

    MYNUM_IO.PUT (USER_NUM);
    NEW_LINE;

    -- Now update the string index
    STRIX := STRIX + LLAST + 1;
  end loop;

exception
  when NULL_INPUT =>
    PUT_LINE ("No input - terminating.");
end BLAH;


  Hope this helps!!

--
=========================================================================
gentle@cnj.digex.net
 
		AMAZING BUT TRUE ...

If all the salmon caught in Canada in one year were laid end to end
across the Sahara Desert, the smell would be absolutely awful.
=========================================================================



  parent reply	other threads:[~1994-09-27 12:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-09-26 14:22 How to convert string 2 int Paul Baptista
1994-09-26 23:25 ` R. William Beckwith
1994-09-27 12:00 ` Gentle [this message]
1994-09-29 16:20 ` John J Cupak Jr CCP
  -- strict thread matches above, loose matches on Subject: below --
1994-09-27 13:48 Keith Arthurs
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox