comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Converting Type Characters to type string
Date: Mon, 31 Mar 2008 08:41:25 -0700 (PDT)
Date: 2008-03-31T08:41:25-07:00	[thread overview]
Message-ID: <eb663170-0f6a-4f7c-9360-78c662c758fa@d21g2000prf.googlegroups.com> (raw)
In-Reply-To: 5052229e-61c3-401d-93b6-a95edee29e58@m71g2000hse.googlegroups.com

On Mar 31, 7:44 am, jedivaughn <jedivaugh...@gmail.com> 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




  reply	other threads:[~2008-03-31 15:41 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-30 20:04 Converting Type Characters to type string jedivaughn
2008-03-30 20:19 ` Pascal Obry
2008-03-30 21:08   ` jedivaughn
2008-03-30 21:28     ` jimmaureenrogers
2008-03-30 21:38     ` Ludovic Brenta
2008-03-30 21:48   ` Georg Bauhaus
2008-03-30 23:52     ` jedivaughn
2008-03-31  3:04       ` george.priv
2008-03-31  4:00         ` tmoran
2008-03-31  8:54           ` Ludovic Brenta
2008-03-31  9:59             ` Dmitry A. Kazakov
2008-03-31 10:59               ` Jean-Pierre Rosen
2008-03-31 13:50                 ` jedivaughn
2008-03-31 14:11                   ` Ludovic Brenta
2008-03-31 14:21                   ` Dmitry A. Kazakov
2008-03-31 14:44                     ` jedivaughn
2008-03-31 15:41                       ` Adam Beneschan [this message]
2008-03-31 20:26                         ` Maciej Sobczak
2008-03-31 22:06                           ` Georg Bauhaus
2008-03-31 22:33                           ` Adam Beneschan
2008-04-01  1:00                             ` jedivaughn
2008-04-01  5:34                               ` Simon Wright
2008-04-01 11:22                                 ` jedivaughn
2008-04-01 12:00                                   ` jimmaureenrogers
2008-04-01 13:22                                     ` jedivaughn
2008-04-01 17:03                                       ` Adam Beneschan
2008-04-01 21:11                                   ` Simon Wright
2008-04-01 22:22                                     ` jedivaughn
2008-04-03  5:54                                   ` tmoran
2008-04-03 14:38                                     ` Adam Beneschan
2008-04-01 16:58                               ` Adam Beneschan
2008-03-31 15:45                       ` Dmitry A. Kazakov
replies disabled

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