Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ada > Re: Converting ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 27 Topic 5627 of 5827
Post > Topic >>

Re: Converting Type Characters to type string

by "Dmitry A. Kazakov" <mailbox@[EMAIL PROTECTED] > Mar 31, 2008 at 05:45 PM

On Mon, 31 Mar 2008 07:44:52 -0700 (PDT), jedivaughn wrote:

> Ok, Let me start over. Maybe I'm not approaching my problem the right
> way. I'm trying to write a program that converts integers to roman
> numerals or vice versa depending on the user input. to do this I
> thought I would get the input in separate characters so that I can
> easily compare the values to find whether the user inputed IX or XI or
> something else. This works until unless the user wants to convert an
> integer to roman numeral. to change the input to an integer I'm using
> this int:=Integer'Value(str); But for this to work the input has to be
> in a string not a character. I thought about taking the input as a
> string and then doing a string slice on the characters and storing
> them in different variables but once again I'm not sure how many
> characters will be entered so how can I do that? So how would be the
> optimal way of going about this?

Sorry, I still do not understand the problem. You read a line. Then you
skip spaces there. Then you try to parse an integer number starting from
that position. If that fails you try to parse a Roman number. After
successful parsing you advance to the position following the number and
skip spaces again. Then you check if the whole like was matched. That's
it.

Here is a complete program:
--------------------------
with Ada.Text_IO;              use Ada.Text_IO;
with Strings_Edit.Integers;    use Strings_Edit.Integers;
with Strings_Edit.Roman_Edit;  use Strings_Edit.Roman_Edit;

procedure Arabic_Roman_Converter is
begin
   loop
      Put_Line ("Enter a number:");
      declare
         Input  : constant String := Get_Line;
         Number : Integer;
      begin
         exit when Input'Length = 0;
         begin
            Number := Value (Input);
            Put_Line ("You typed " & Image (Roman (Number)));
         exception
            when End_Error =>
               -- OK, no integer here, maybe it is a Roman number?
               Number := Integer (Roman'(Value (Input)));
               Put_Line ("You typed " & Image (Number));
         end;
      exception
         when Data_Error =>
            Put_Line ("Bad input, possibly syntax or several numbers");
         when Constraint_Error =>
            Put_Line ("The number is too large");
         when End_Error =>
            Put_Line ("No any number found");
      end;
   end loop;
end Arabic_Roman_Converter;   
-----------------------------------
Enter a number:
 II
You typed 2
Enter a number:
3
You typed III
Enter a number:
1000
You typed M
Enter a number:
f
No any number found
Enter a number:


-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
 




 27 Posts in Topic:
Re: Converting Type Characters to type string
Georg Bauhaus <rm.plus  2008-03-30 23:48:32 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-03-30 16:52:58 
Re: Converting Type Characters to type string
george.priv@[EMAIL PROTEC  2008-03-30 20:04:38 
Re: Converting Type Characters to type string
tmoran@[EMAIL PROTECTED]   2008-03-30 23:00:17 
Re: Converting Type Characters to type string
Ludovic Brenta <ludovi  2008-03-31 01:54:24 
Re: Converting Type Characters to type string
"Dmitry A. Kazakov&q  2008-03-31 11:59:30 
Re: Converting Type Characters to type string
Jean-Pierre Rosen <ros  2008-03-31 12:59:28 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-03-31 06:50:32 
Re: Converting Type Characters to type string
"Dmitry A. Kazakov&q  2008-03-31 16:21:36 
Re: Converting Type Characters to type string
Ludovic Brenta <ludovi  2008-03-31 07:11:07 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-03-31 07:44:52 
Re: Converting Type Characters to type string
"Dmitry A. Kazakov&q  2008-03-31 17:45:21 
Re: Converting Type Characters to type string
Adam Beneschan <adam@[  2008-03-31 08:41:25 
Re: Converting Type Characters to type string
Maciej Sobczak <see.my  2008-03-31 13:26:36 
Re: Converting Type Characters to type string
Georg Bauhaus <rm.tsoh  2008-04-01 00:06:22 
Re: Converting Type Characters to type string
Adam Beneschan <adam@[  2008-03-31 15:33:45 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-03-31 18:00:03 
Re: Converting Type Characters to type string
Simon Wright <simon.j.  2008-04-01 06:34:17 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-04-01 04:22:55 
Re: Converting Type Characters to type string
Simon Wright <simon.j.  2008-04-01 22:11:26 
Re: Converting Type Characters to type string
tmoran@[EMAIL PROTECTED]   2008-04-03 00:54:01 
Re: Converting Type Characters to type string
Adam Beneschan <adam@[  2008-04-03 07:38:57 
Re: Converting Type Characters to type string
"jimmaureenrogers@[E  2008-04-01 05:00:49 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-04-01 06:22:49 
Re: Converting Type Characters to type string
Adam Beneschan <adam@[  2008-04-01 09:58:20 
Re: Converting Type Characters to type string
Adam Beneschan <adam@[  2008-04-01 10:03:47 
Re: Converting Type Characters to type string
jedivaughn <jedivaughn  2008-04-01 15:22:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 0:02:30 CDT 2008.