From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4c78d647a27bc3e1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-09-27 05:06:04 PST Path: bga.com!news.sprintlink.net!uunet!news1.digex.net!cnj.digex.net!cnj!gentle From: gentle@cnj.digex.net (Gentle) Newsgroups: comp.lang.ada Subject: Re: How to convert string 2 int... Date: 27 Sep 1994 12:00:30 GMT Organization: Express Access Online Communications, Greenbelt, MD USA Message-ID: <3691gu$kp4@cnj.digex.net> References: NNTP-Posting-Host: cnj.digex.net X-Newsreader: TIN [version 1.2 PL2] Date: 1994-09-27T12:00:30+00:00 List-Id: 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. =========================================================================