comp.lang.ada
 help / color / mirror / Atom feed
* newbie question: String to Integer conversion
@ 1999-12-09  0:00 Noam Kloos
  1999-12-09  0:00 ` David C. Hoos, Sr.
  1999-12-09  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 3+ messages in thread
From: Noam Kloos @ 1999-12-09  0:00 UTC (permalink / raw)


Hello, 

I tried to make  a program and it worked. However I also tried to use
Ada.Command_Line package. My argument must be an integer, but
Ada.Command_Line passes a String. I do not know how to convert a
string into an Integer.

Tried to look through the adainclude directory but came up with
nothing.

I am using an i386 redhat 6.0 gnat-3.12 ada compiler.

My recusive program can be found as a zipfile  at
www.bigfoot.com/~nkloos/ada.htm

Greetings and hopefully Thanks for giving me the solution..

Noam Kloos.






^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: newbie question: String to Integer conversion
  1999-12-09  0:00 newbie question: String to Integer conversion Noam Kloos
  1999-12-09  0:00 ` David C. Hoos, Sr.
@ 1999-12-09  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1999-12-09  0:00 UTC (permalink / raw)


In article <OsUkROpQ$GA.331@net003s> , noam@interspective.nl (Noam 
Kloos) wrote:

> I tried to make  a program and it worked. However I also tried to use
> Ada.Command_Line package. My argument must be an integer, but
> Ada.Command_Line passes a String. I do not know how to convert a
> string into an Integer.

All scalar types (including type Integer) come with an attribute

  T'Value

which takes a string and returns a value of the (scalar) type.  See the
appendix re "predefined language attributes" in your RM.

Note also that T'Value accepts a string that has the syntax of a based
literal, something like:

  I := Integer'Value ("16#ABCD#");

Occasionally I meet Ada programmers who don't know this, and proceed to
write their own conversion algorithm.

If this is terminal input, you don't have to require that the user
specify the complete syntax; you can fill it for him:

  Put ("Enter hex value: ");
  Get_Line (Line, Last);

  Value := Integer'Value ("16#" & Line (1 .. Last) & "#");

This way, the user only has to enter "abcd", instead of "16#abcd#".


--
Why stop at evolution and cosmology, though? Let's make sure that the
schoolkids of Kansas get a really first-rate education by loosening up
the teaching standards for other so-called scientific ideas that are,
after all, just theories. The atomic theory, for example. The theory of
relativity. Heck, the Copernican theory--do we really know that the
universe doesn't revolve around the earth?

John Rennie, Scientific American, Oct 1999




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: newbie question: String to Integer conversion
  1999-12-09  0:00 newbie question: String to Integer conversion Noam Kloos
@ 1999-12-09  0:00 ` David C. Hoos, Sr.
  1999-12-09  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: David C. Hoos, Sr. @ 1999-12-09  0:00 UTC (permalink / raw)



Noam Kloos <noam@interspective.nl> wrote in message
news:OsUkROpQ$GA.331@net003s...
> Hello,
>
> I tried to make  a program and it worked. However I also tried to use
> Ada.Command_Line package. My argument must be an integer, but
> Ada.Command_Line passes a String. I do not know how to convert a
> string into an Integer.
>
> Tried to look through the adainclude directory but came up with
> nothing.
>
> I am using an i386 redhat 6.0 gnat-3.12 ada compiler.
>
> My recusive program can be found as a zipfile  at
> www.bigfoot.com/~nkloos/ada.htm
>
> Greetings and hopefully Thanks for giving me the solution..
>
For every scalar type T the attribute function T'Value (X : String)
returns the value represented by X, if X represents a value of the
type.  Otherwise Constraint_Error is raised.

So... Integer'Value ("1234") returns the integer 1234.







^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-12-09  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-09  0:00 newbie question: String to Integer conversion Noam Kloos
1999-12-09  0:00 ` David C. Hoos, Sr.
1999-12-09  0:00 ` Matthew Heaney

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