comp.lang.ada
 help / color / mirror / Atom feed
* Input a string
@ 2005-01-27 20:27 Stefan Merwitz
  2005-01-27 22:37 ` Nick Roberts
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Merwitz @ 2005-01-27 20:27 UTC (permalink / raw)


Hello everybody,

I need to input a string from the user. For integers I use 
Get(IntegerType). But unfortunately this doesn't seem to work for 
strings ("unconstrained subtype not allowed"). How do I input a string 
and futhermore how to return the first 30 letters if the string is 
longer than 30 chars?


Thanks in advance,

Stefan



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

* Re: Input a string
  2005-01-27 20:27 Input a string Stefan Merwitz
@ 2005-01-27 22:37 ` Nick Roberts
  2005-01-28 14:56   ` Stefan Merwitz
  0 siblings, 1 reply; 4+ messages in thread
From: Nick Roberts @ 2005-01-27 22:37 UTC (permalink / raw)


Stefan Merwitz <thebiggestfan2002@yahoo.de> wrote:

> I need to input a string from the user. For integers I use
> Get(IntegerType). But unfortunately this doesn't seem to work for strings
> ("unconstrained subtype not allowed"). How do I input a string and
> futhermore how to return the first 30 letters if the string is longer than
> 30 chars?

I'll assume I'm not doing your homework for you :-)

One simple soultion is:

      Description: String(1..30); -- constrained
      Desc_Length: Natural range 0..Description'Last;
      ...
   begin
      ...
      Get_Line(Description,Desc_Length);
      if Desc_Length = Description'Last then Skip_Line; end if;

If the (remaining) text on the current line is less than 30 characters, the
line will be read into Description(1..Desc_Length). If the text is longer
than 30 characters, the first 30 will be put into Description, and the rest
will be skipped, and Desc_Length will be set to 30. Either way, the file
pointer will be moved to the beginning of the next line. (Untested.)

Does this help you? There are certainly various other ways to do it.

-- 
Nick Roberts



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

* Re: Input a string
  2005-01-27 22:37 ` Nick Roberts
@ 2005-01-28 14:56   ` Stefan Merwitz
  2005-01-29  8:52     ` Stefan Merwitz
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Merwitz @ 2005-01-28 14:56 UTC (permalink / raw)


Thanks, Nick

But I have another problem: If your code is run after a Get(Character), 
the user doesn't get the possibilty to enter anything in your code. I 
guess there's some kind of key buffer that hasn't been emptied. How do I 
prevent the program from skipping your source?

Source:

        Description: String(1..30); -- constrained
        Desc_Length: Natural range 0..Description'Last;
        ...
     begin
        ...
        Get(Character_Type);
        Get_Line(Description,Desc_Length);
        if Desc_Length = Description'Last then Skip_Line; end if;


Thanks in advance,

Stefan
Nick Roberts schrieb:
> Stefan Merwitz <thebiggestfan2002@yahoo.de> wrote:
> 
> 
>>I need to input a string from the user. For integers I use
>>Get(IntegerType). But unfortunately this doesn't seem to work for strings
>>("unconstrained subtype not allowed"). How do I input a string and
>>futhermore how to return the first 30 letters if the string is longer than
>>30 chars?
> 
> 
> I'll assume I'm not doing your homework for you :-)
> 
> One simple soultion is:
> 
>       Description: String(1..30); -- constrained
>       Desc_Length: Natural range 0..Description'Last;
>       ...
>    begin
>       ...
>       Get_Line(Description,Desc_Length);
>       if Desc_Length = Description'Last then Skip_Line; end if;
> 
> If the (remaining) text on the current line is less than 30 characters, the
> line will be read into Description(1..Desc_Length). If the text is longer
> than 30 characters, the first 30 will be put into Description, and the rest
> will be skipped, and Desc_Length will be set to 30. Either way, the file
> pointer will be moved to the beginning of the next line. (Untested.)
> 
> Does this help you? There are certainly various other ways to do it.
> 



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

* Re: Input a string
  2005-01-28 14:56   ` Stefan Merwitz
@ 2005-01-29  8:52     ` Stefan Merwitz
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Merwitz @ 2005-01-29  8:52 UTC (permalink / raw)


Found the error, needs Skip_Line before Get_Line.



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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-27 20:27 Input a string Stefan Merwitz
2005-01-27 22:37 ` Nick Roberts
2005-01-28 14:56   ` Stefan Merwitz
2005-01-29  8:52     ` Stefan Merwitz

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