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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,7776df5b2a2fcc30 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!p37g2000pra.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Beginner Question Date: Wed, 20 Oct 2010 15:45:23 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <90ceb514-83aa-44c2-b253-218956d8426d@d17g2000yqm.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1287614724 27991 127.0.0.1 (20 Oct 2010 22:45:24 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 20 Oct 2010 22:45:24 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p37g2000pra.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:15608 Date: 2010-10-20T15:45:23-07:00 List-Id: On Oct 20, 2:41=A0pm, SpoonThief wrote: > So, I have a question that may seem painfully obvious, but how does > one go about parsing a Float from a string that has other data in it > too? I'm getting a line from the keyboard that has a float and a > custom type in it, and I need to separate the two values. I'm using a > call to Get_Line to get the entire line the user entered as a string, > and when I try to call Float'Value (In_String (1 .. Last)), I get a > constraint error. Is there any way to parse a float and a custom > subtype (Excellent, Good, Fair, Poor) from one string, or am I going > about this the entirely wrong way? Any help would be much appreciated, > and if clarification is needed because I wasn't clear, just ask. Thank > you in advance! Float'Value expects the entire string to look like a float. The Float_IO generic in Ada.Text_IO has a Get routine that will let you parse a float off the beginning of the string. (If you're using the predefined Float type, Ada.Float_Text_IO is a ready-made instantiation of Float_IO.) Similarly, there's an Enumeration_IO generic in Ada.Text_IO that will let you parse your custom enumeration type. Both routines return a Last parameter that gives you the index of the last character it used. So something like this should work, if T is your enumeration type: The_Float : Float; The_T : T; F_Last : Positive; T_Last : Positive; package T_IO is new Ada.Text_IO.Enumeration_IO (T); Ada.Float_Text_IO.Get (In_String (1 .. Last), The_Float, F_Last); T_IO.Get (In_String (F_Last + 1 .. Last), The_T, T_Last); More info is in RM sections A.10.9 and A.10.10. Those are simple ways to get things to work. If you need more control over the parsing, check out the operations in Ada.Strings.Fixed (RM A. 4.3); you could use those to search for blanks or whatever other character you need to, and then once you figure out the beginning and ending indexes of the floating-point part of the input, say, then you can use Float'Value (In_String (Begin_Index .. End_Index)). -- Adam