* How to convert string 2 int...
@ 1994-09-26 14:22 Paul Baptista
1994-09-26 23:25 ` R. William Beckwith
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Paul Baptista @ 1994-09-26 14:22 UTC (permalink / raw)
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.
Ex. Enter string: 1x2x3x4x5x6x7x8x9x10
Result:
1
2
3
4
5
6
7
8
9
10
---
55
Can anyone help me?
TIA!!
Pauloo
bub@cis.umassd.edu
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to convert string 2 int...
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
1994-09-29 16:20 ` John J Cupak Jr CCP
2 siblings, 0 replies; 5+ messages in thread
From: R. William Beckwith @ 1994-09-26 23:25 UTC (permalink / raw)
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.
snip
: Can anyone help me?
Is this a homework assignment?
... Bill
--
e-mail: Bill.Beckwith@ois.com | Team Ada
Objective Interface Systems, Inc. | dist, full O-O
1895 Preston White Drive, Suite 250 | multithreading
Reston, VA 22091-5448 U.S.A. | built in
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to convert string 2 int...
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
1994-09-29 16:20 ` John J Cupak Jr CCP
2 siblings, 0 replies; 5+ messages in thread
From: Gentle @ 1994-09-27 12:00 UTC (permalink / raw)
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.
=========================================================================
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to convert string 2 int...
@ 1994-09-27 13:48 Keith Arthurs
0 siblings, 0 replies; 5+ messages in thread
From: Keith Arthurs @ 1994-09-27 13:48 UTC (permalink / raw)
In article <CwqqL5.CEv@umassd.edu>,
on Mon, 26 Sep 1994 14:22:17 GMT,
Paul Baptista <bub@cis.umassd.edu> writes:
>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?
>
[snip]
>
>Pauloo
>
>bub@cis.umassd.edu
You could use the 'Value attribute for converting String to Integer. Look
up 'Value in section 3.5.5 in the LRM.
Keith Arthurs
McDonnell Douglas Corporation
Disclaimer: De vus her ur net deup en yuns uv mhik denhuld uhglus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to convert string 2 int...
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
@ 1994-09-29 16:20 ` John J Cupak Jr CCP
2 siblings, 0 replies; 5+ messages in thread
From: John J Cupak Jr CCP @ 1994-09-29 16:20 UTC (permalink / raw)
Paul,
Given the simplicity of this problem, I assume that this is a programming assignment. I suspect that the instructor wants you to parse
an input line an break it into tokens, displaying the numeric ones.
Well, I won't tell you how to do all of it, but take a look at
the data conversion attributes in LRM Appendix A.
Hope this helps!
--
John J. Cupak, Jr., CCP Lockheed DECNet: NHQVAX::CUPAK
Lockheed Sanders, Inc. Internet : cupak@rapnet.sanders.LOCKHEED.COM
95 Canal Street / MER15-2403 CompuServe : 72411.3176@compuserve.com
Nashua, NH 03061-0868 Telephone : (603) 885-2142 FAX: 885-1480
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1994-09-29 16:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox