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 13 of 27 Topic 5627 of 5922
Post > Topic >>

Re: Converting Type Characters to type string

by Adam Beneschan <adam@[EMAIL PROTECTED] > Mar 31, 2008 at 08:41 AM

On Mar 31, 7:44 am, jedivaughn <jedivaugh...@[EMAIL PROTECTED]
> 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.

Perhaps I'm not understanding you correctly, but I don't see why you
need to get the input in separate characters, or why that would be
helpful.  You can always get the input as an entire string, using
Ada.Text_IO.Get_Line, and then *process* the input as separate
characters.

I almost never use single-character input.  The only time I would want
to is if, for some reason, you need the program to react immediately
to some key that's typed in (other than Return or Enter).  E.g. I've
written programs that will do something interesting if the user
presses the up-arrow or down-arrow keys on the keyboard.  That was
done using the Get routine.  If you don't do anything like that, then
you should use Get_Line.  (For one thing, you don't have to worry
about handling the user's backspaces.)

So your code might look something like:

    Ada.Text_IO.Get_Line (Input_Str, Last_Index);
    for Index in Input_Str'First .. Last_Index loop
        ... Input_Str(Index) will be the character you're looking
at...
    end loop;

or, if you want to examine characters outside the loop:

    Ada.Text_IO.Get_Line (Input_Str, Last_Index);
    Curr_Index := Input_Str'First;
    while Curr_Index <= Last_Index and then Input_Str(Curr_Index) = '
' loop
       Curr_Index := Curr_Index + 1;
    end loop;
    ... special handling for case where Curr_Index > Last_Index!  The
    ... user entered an blank string
    if Input_Str(Curr_Index) = 'I' or else
       Input_Str(Curr_Index) = 'V' ... then
    ... it's better to use an array of Boolean for this
       Do_Something_With_Roman_Numeral (Input_Str
(Input_Str'First ..
                                                   Last_Index));
    elsif Input_Str(Curr_Index) in '0'..'9' then
       Do_Something_With_Integer (Input_Str (Input_Str'First ..
Last_Index));
    else
       ... ??? user error

OK, this is just an example of how you might handle things; I really
am not sure what sort of processing you're trying to do.  But my point
is to show that you probably ought to be using Get_Line and not
worrying about how to input one character at a time.

                                     -- Adam
 




 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 Mon Oct 6 18:27:05 CDT 2008.