comp.lang.ada
 help / color / mirror / Atom feed
* Input a string (again)
@ 2005-01-29  8:55 Stefan Merwitz
  2005-01-29  9:53 ` Adrian Knoth
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stefan Merwitz @ 2005-01-29  8:55 UTC (permalink / raw)


Hello everybody,

I'm nearly at despairing. After I'm now able to input a string (30 
chars), I need to cancel the loop in the following source, when the user 
enters an empty line:

     function InputString return string is
         sString: String(1..30); -- constrained
         iString_Length: Natural range 0..sString'Last;
     begin
         Skip_Line;
         Get_Line(sString, iString_Length);
         if iString_Length = sString'Last then
             Skip_Line;
         end if;
         return sString;
     end;

     bContinue: boolean := true;
     begin
         while bContinue loop
             sHobbyStr := InputString;
             if Trim(To_Unbounded_String(sHobbyStr), Both) = "" then
                 bContinue := false;
             else
                 Put(sHobbyStr);
             end if;
         end loop;
     end;

But unfortunately, neither
     Trim(To_Unbounded_String(sHobbyStr), Both) = ""
nor
     sHobbyStr = "                              " (30 empty chars)
work.

All I want is to cancel that loop when nothing is entered, help!


Could you please help me,

Stefan



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

* Re: Input a string (again)
  2005-01-29  8:55 Input a string (again) Stefan Merwitz
@ 2005-01-29  9:53 ` Adrian Knoth
  2005-01-29 12:51 ` Stefan Merwitz
  2005-01-29 21:03 ` tmoran
  2 siblings, 0 replies; 4+ messages in thread
From: Adrian Knoth @ 2005-01-29  9:53 UTC (permalink / raw)


Stefan Merwitz <thebiggestfan2002@yahoo.de> wrote:

Hi!

>      function InputString return string is
>          sString: String(1..30); -- constrained

Why don't you use an unbounded string?

> All I want is to cancel that loop when nothing is entered, help!

with Ustrings; use Ustrings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure bla is
   HobbyString : Ustring;
begin
   loop
      Get_Line (HobbyString);
      exit when Empty (HobbyString);
      Put_Line (HobbyString);
   end loop;
end bla;

The ustrings-package was written by David A. Wheeler. You can find it
in the adabill-sourcepackage on my homepage (http://adi.thur.de)      
   
If you don't want to use ustrings you may rewrite your program like this:

  function InputString return String is
     buffer : String (1 .. 30);
     iLength : Positive;
  begin
     Get_Line (buffer, iLength)
     return buffer (buffer'First .. iLength);
  end InputString;

begin
   loop
      declare
         something : String := InputString;
      begin
         exit when something = "";
         Put_Line (something);
      end;
   end loop;
end everything;


-- 
mail: adi@thur.de  	http://adi.thur.de	PGP: v2-key via keyserver

Alle Kinder stehen am Abgrund, nur nicht Peter, der ging noch'n Meter.



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

* Re: Input a string (again)
  2005-01-29  8:55 Input a string (again) Stefan Merwitz
  2005-01-29  9:53 ` Adrian Knoth
@ 2005-01-29 12:51 ` Stefan Merwitz
  2005-01-29 21:03 ` tmoran
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Merwitz @ 2005-01-29 12:51 UTC (permalink / raw)


thanks, am now using unbounded_strings with my records (see threat 
"access types to records containing strings").


Thanks again,

Stefan



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

* Re: Input a string (again)
  2005-01-29  8:55 Input a string (again) Stefan Merwitz
  2005-01-29  9:53 ` Adrian Knoth
  2005-01-29 12:51 ` Stefan Merwitz
@ 2005-01-29 21:03 ` tmoran
  2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2005-01-29 21:03 UTC (permalink / raw)


> After I'm now able to input a string (30 chars),
> ...
>         Get_Line(sString, iString_Length);
> ...
>         return sString;
  "Get_Line" really ought to be renamed "Read_Into_Buffer" or something.
Don't think of sString as your input string, think of it as a buffer
that will hold up to 30 characters of input.  The actual string typed
in by the user is in the first iString_Length positions in the buffer,
so sString(1 .. iString_Length) is the typed input.
  BTW, in Ada strings don't necessarily have to start with an index
of 1.  For instance, a procedure might be called with a string parameter
which is position 5 .. 10 of some larger string.  So it's safer to be
in the habit of not assuming a start index of 1.  Thus you should have:
          Get_Line(sString, iString_Last);
and
          return sString(sString'first .. iString_Last);



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

end of thread, other threads:[~2005-01-29 21:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-29  8:55 Input a string (again) Stefan Merwitz
2005-01-29  9:53 ` Adrian Knoth
2005-01-29 12:51 ` Stefan Merwitz
2005-01-29 21:03 ` tmoran

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