comp.lang.ada
 help / color / mirror / Atom feed
* Problems with Ada.Text_IO and strings.
@ 2003-10-13 22:23 Bleakcabal
  2003-10-13 23:51 ` Larry Hazel
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Bleakcabal @ 2003-10-13 22:23 UTC (permalink / raw)


Hi, im new to Ada and I am writing my first program trying to "get"
Ada.

My small program ask for input from the user. I read it using :

Ada.Text_IO.Get_Line (MyType.Name, NameLength);

This data is then stored inside an array of a type I have declared :

type MyType is record
�����name : string(1..20);
�����adress : string(1..40);
�����telephone : natural;
end record;

Later in the program I output this information using :
Ada.Text_IO.Put (MyType.name);

My problem is this : when I output the name, part of the string is
filled with "garbage" characters. If I fill the string with 20
characters I get no garbage. But when I write a name that's about 10
characters long, I get about 10 characters of garbage.

Im using Gnat as my Ada compiler if that makes a difference.

Any help would be appreciated.

Thank you in advance.



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
@ 2003-10-13 23:51 ` Larry Hazel
  2003-10-14  0:06 ` Chad R. Meiners
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 27+ messages in thread
From: Larry Hazel @ 2003-10-13 23:51 UTC (permalink / raw)


Bleakcabal wrote:

> Hi, im new to Ada and I am writing my first program trying to "get"
> Ada.
> 
> My small program ask for input from the user. I read it using :
> 
> Ada.Text_IO.Get_Line (MyType.Name, NameLength);
> 
> This data is then stored inside an array of a type I have declared :
> 
> type MyType is record
>      name : string(1..20);
>      adress : string(1..40);
>      telephone : natural;
> end record;
> 
> Later in the program I output this information using :
> Ada.Text_IO.Put (MyType.name);
> 
> My problem is this : when I output the name, part of the string is
> filled with "garbage" characters. If I fill the string with 20
> characters I get no garbage. But when I write a name that's about 10
> characters long, I get about 10 characters of garbage.
> 
> Im using Gnat as my Ada compiler if that makes a difference.
> 
> Any help would be appreciated.
> 
> Thank you in advance.
First off, you need a variable of type MyType to read into, but I expect 
you do have that.  Ada.Text_IO.Get_Line reads NameLength characters into 
the name field.  If you enter 10 characters, then NameLength is 10 and 
MyRecord.Name(11..20) will be unchanged, probably garbage characters.




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
  2003-10-13 23:51 ` Larry Hazel
@ 2003-10-14  0:06 ` Chad R. Meiners
  2003-10-14 12:32   ` Bleakcabal
  2003-10-14  1:29 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 27+ messages in thread
From: Chad R. Meiners @ 2003-10-14  0:06 UTC (permalink / raw)



"Bleakcabal" <bleakcabal@gdnmail.net> wrote in message
news:8336eb21.0310131423.7410e1ec@posting.google.com...
> Hi, im new to Ada and I am writing my first program trying to "get"
> Ada.
>
> My small program ask for input from the user. I read it using :
>
> Ada.Text_IO.Get_Line (MyType.Name, NameLength);
>
> This data is then stored inside an array of a type I have declared :
>
> type MyType is record
> name : string(1..20);
> adress : string(1..40);
> telephone : natural;
> end record;

> Later in the program I output this information using :
> Ada.Text_IO.Put (MyType.name);

Try

Ada.Text_IO.Put(MyType.name(1..Namelength));  -- to print only the part of
the string that was used.

> My problem is this : when I output the name, part of the string is
> filled with "garbage" characters. If I fill the string with 20
> characters I get no garbage. But when I write a name that's about 10
> characters long, I get about 10 characters of garbage.
>
> Im using Gnat as my Ada compiler if that makes a difference.
>
> Any help would be appreciated.
>
> Thank you in advance.

btw, is this for a class?

-CRM





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
  2003-10-13 23:51 ` Larry Hazel
  2003-10-14  0:06 ` Chad R. Meiners
@ 2003-10-14  1:29 ` Jeffrey Carter
  2003-10-14 12:39   ` Bleakcabal
  2003-10-14 12:33 ` Bleakcabal
  2003-10-15  6:25 ` CheGueVerra
  4 siblings, 1 reply; 27+ messages in thread
From: Jeffrey Carter @ 2003-10-14  1:29 UTC (permalink / raw)


Bleakcabal wrote:

> Hi, im new to Ada and I am writing my first program trying to "get"
> Ada.
> 
> My small program ask for input from the user. I read it using :
> 
> Ada.Text_IO.Get_Line (MyType.Name, NameLength);
> 
> This data is then stored inside an array of a type I have declared :
> 
> type MyType is record
>      name : string(1..20);
>      adress : string(1..40);
>      telephone : natural;
> end record;
> 
> Later in the program I output this information using :
> Ada.Text_IO.Put (MyType.name);
> 
> My problem is this : when I output the name, part of the string is
> filled with "garbage" characters. If I fill the string with 20
> characters I get no garbage. But when I write a name that's about 10
> characters long, I get about 10 characters of garbage.

Mytype.Name is 20 characters long. It is always 20 characters long, 
regardless of what those characters are. "Put (X.Name);" (assuming "X : 
Mytype;"; what you have is illegal) outputs all 20 characters. If you 
haven't assigned to all 20 characters, then some of them contain junk.

Your problem is that you are not storing the length that you get from 
Get_Line. (Technically, this is the index of the last position filled, 
not the length. This is the same as the length in your case because the 
lower bound is one, but there's no requirement that it be one; it could 
just as well be 2 or 17.)

Your choices include storing the length and using it to slice the 
string, or using Ada.Strings.[Un]Bounded.

You'll probably find another kind of unexpected behavior if you enter 
more than 20 characters for the name.

You need to read the documentation for Get_Line and be sure you 
understand what it does for a line with fewer characters than the 
string, the same number of characters as the string, and more characters 
than the string.

You also have to understand that String in Ada is simply a 
one-dimensional array of characters, no different than any other array. 
A String always has the same number of characters, regardless of what 
you put in it.

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14  0:06 ` Chad R. Meiners
@ 2003-10-14 12:32   ` Bleakcabal
  2003-10-14 15:03     ` Robert I. Eachus
  0 siblings, 1 reply; 27+ messages in thread
From: Bleakcabal @ 2003-10-14 12:32 UTC (permalink / raw)


> 
> btw, is this for a class?
> 
> -CRM


Actually I switched from one college to another to continue my CS
program in another. In my first college I programmed with C/C++ and
Java both are languages I had used before and consider myself
proficient in. But in the is new college my programation classes are
in Ada and since my programmation classes from that other college were
credited I am not starting Ada from the basics. This would not be a
problem, but I decided to wait for the Ada book the class used at the
college library but it is not in print currently and after more than
1/3 of the semester it doesn't seem like the book is going to arrive
soon.

So I am basicly learning Ada of the net right now, I think I will buy
a book since I have problems with reading strings and all :) I think
the class book will never be ready for the end of the semester :)



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
                   ` (2 preceding siblings ...)
  2003-10-14  1:29 ` Jeffrey Carter
@ 2003-10-14 12:33 ` Bleakcabal
  2003-10-15  6:25 ` CheGueVerra
  4 siblings, 0 replies; 27+ messages in thread
From: Bleakcabal @ 2003-10-14 12:33 UTC (permalink / raw)


Thank you for your awnsers.



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14  1:29 ` Jeffrey Carter
@ 2003-10-14 12:39   ` Bleakcabal
  2003-10-14 12:57     ` sk
                       ` (2 more replies)
  0 siblings, 3 replies; 27+ messages in thread
From: Bleakcabal @ 2003-10-14 12:39 UTC (permalink / raw)


It would have been great if Ada strings read the end of line character
and Put took care of them so the lines were automagicly sliced like C
strings.



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14 12:39   ` Bleakcabal
@ 2003-10-14 12:57     ` sk
  2003-10-14 14:14     ` Problems with Ada.Text_IO and strings. (wants EOL character) Larry Kilgallen
  2003-10-14 20:19     ` Problems with Ada.Text_IO and strings Jeffrey Carter
  2 siblings, 0 replies; 27+ messages in thread
From: sk @ 2003-10-14 12:57 UTC (permalink / raw)
  To: comp.lang.ada

Bleakcabal <bleakcabal@gdnmail.net>:
 > It would have been great if Ada strings read the end of line
 > character and Put took care of them so the lines were
 > automagicly sliced like C strings.

declare

     Buffer  : String (1 .. Max_Buffer) := (Others => ' ');
     BufLast : Natural := 0;

begin
     TIO.Get_Line (Buffer, BufLast);
     TIO.Put_Line (Buffer(Buffer'First .. BufLast));
end;


-- 
-------------------------------------------------
-- Merge vertically for real address
--
--     s n p @ t . o
--      k i e k c c m
-------------------------------------------------




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

* Re: Problems with Ada.Text_IO and strings. (wants EOL character)
  2003-10-14 12:39   ` Bleakcabal
  2003-10-14 12:57     ` sk
@ 2003-10-14 14:14     ` Larry Kilgallen
  2003-10-14 16:20       ` Stephen Leake
  2003-10-14 20:19     ` Problems with Ada.Text_IO and strings Jeffrey Carter
  2 siblings, 1 reply; 27+ messages in thread
From: Larry Kilgallen @ 2003-10-14 14:14 UTC (permalink / raw)


In article <8336eb21.0310140439.393f2e8@posting.google.com>, bleakcabal@gdnmail.net (Bleakcabal) writes:

> It would have been great if Ada strings read the end of line character
> and Put took care of them so the lines were automagicly sliced like C
> strings.

That would certainly prevent your programs from working in environments
where there is no end of line character, such as a sequential RMS file
on VMS.



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14 12:32   ` Bleakcabal
@ 2003-10-14 15:03     ` Robert I. Eachus
  2003-10-14 15:16       ` Stephane Richard
  0 siblings, 1 reply; 27+ messages in thread
From: Robert I. Eachus @ 2003-10-14 15:03 UTC (permalink / raw)


Bleakcabal wrote:

> So I am basicly learning Ada of the net right now, I think I will buy
> a book since I have problems with reading strings and all :) I think
> the class book will never be ready for the end of the semester :)

There are several good Ada books available on the web, including the Ada 
Reference Manual.  I'll let others recommend on-line and published 
textbooks, but the ARM can be found here: 
http://www.ada-auth.org/~acats/arm.html  It is a standard, not the best 
format for learning the language, but since it includes things like the 
standard libraries it allows you to do a quick lookup if you know that 
something is there, but can't remember the calling sequence.  In this 
particular case, Ada.Strings.Unbounded can help you with your problem. 
(If you use the GNAT compiler, it even has an Ada.Text_IO extention to 
read and write Unbounded_String variables.  See the GNAT documentation 
for that.)

-- 
                                                     Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the 
goal of Art. It remains to work these concepts into a practical, 
down-to-earth context, and for this there is nothing more practical or 
down-to-earth than what I have been talking about all along...the repair 
of an old motorcycle."  -- from Zen and the Art of Motorcycle 
Maintenance by Robert Pirsig




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14 15:03     ` Robert I. Eachus
@ 2003-10-14 15:16       ` Stephane Richard
  2003-10-14 20:16         ` Jeffrey Carter
  0 siblings, 1 reply; 27+ messages in thread
From: Stephane Richard @ 2003-10-14 15:16 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1787 bytes --]

Hi Bleakcabal,

Have a look atmy website (http://www.adaworld.com) I have a learning center
that has tutorials AND freely available books in the PDF file format that
might be of interest to you.

-- 
St�phane Richard
"Ada World" Webmaster
http://www.adaworld.com


"Robert I. Eachus" <rieachus@comcast.net> wrote in message
news:3F8C1037.6030005@comcast.net...
> Bleakcabal wrote:
>
> > So I am basicly learning Ada of the net right now, I think I will buy
> > a book since I have problems with reading strings and all :) I think
> > the class book will never be ready for the end of the semester :)
>
> There are several good Ada books available on the web, including the Ada
> Reference Manual.  I'll let others recommend on-line and published
> textbooks, but the ARM can be found here:
> http://www.ada-auth.org/~acats/arm.html  It is a standard, not the best
> format for learning the language, but since it includes things like the
> standard libraries it allows you to do a quick lookup if you know that
> something is there, but can't remember the calling sequence.  In this
> particular case, Ada.Strings.Unbounded can help you with your problem.
> (If you use the GNAT compiler, it even has an Ada.Text_IO extention to
> read and write Unbounded_String variables.  See the GNAT documentation
> for that.)
>
> -- 
>                                                      Robert I. Eachus
>
> "Quality is the Buddha. Quality is scientific reality. Quality is the
> goal of Art. It remains to work these concepts into a practical,
> down-to-earth context, and for this there is nothing more practical or
> down-to-earth than what I have been talking about all along...the repair
> of an old motorcycle."  -- from Zen and the Art of Motorcycle
> Maintenance by Robert Pirsig
>





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

* Re: Problems with Ada.Text_IO and strings. (wants EOL character)
  2003-10-14 14:14     ` Problems with Ada.Text_IO and strings. (wants EOL character) Larry Kilgallen
@ 2003-10-14 16:20       ` Stephen Leake
  2003-10-14 16:45         ` Stephane Richard
  0 siblings, 1 reply; 27+ messages in thread
From: Stephen Leake @ 2003-10-14 16:20 UTC (permalink / raw)


In article <8336eb21.0310140439.393f2e8@posting.google.com>,
bleakcabal@gdnmail.net (Bleakcabal) writes:

> It would have been great if Ada strings read the end of line character
> and Put took care of them so the lines were automagicly sliced like C
> strings.

Ada.Text_IO.Get_Line nicely handles the "end of line character".

I missed your original post; what was your problem? Does Get_Line
solve it?
-- 
-- Stephe



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

* Re: Problems with Ada.Text_IO and strings. (wants EOL character)
  2003-10-14 16:20       ` Stephen Leake
@ 2003-10-14 16:45         ` Stephane Richard
  0 siblings, 0 replies; 27+ messages in thread
From: Stephane Richard @ 2003-10-14 16:45 UTC (permalink / raw)


> I missed your original post; what was your problem? Does Get_Line
> solve it?
> -- 
> -- Stephe

Here's that original Post Steph :-)

------------------------------------------------------------------------
Hi, im new to Ada and I am writing my first program trying to "get"
Ada.

My small program ask for input from the user. I read it using :

Ada.Text_IO.Get_Line (MyType.Name, NameLength);

This data is then stored inside an array of a type I have declared :

type MyType is record
name : string(1..20);
adress : string(1..40);
telephone : natural;
end record;

Later in the program I output this information using :
Ada.Text_IO.Put (MyType.name);

My problem is this : when I output the name, part of the string is
filled with "garbage" characters. If I fill the string with 20
characters I get no garbage. But when I write a name that's about 10
characters long, I get about 10 characters of garbage.

Im using Gnat as my Ada compiler if that makes a difference.

Any help would be appreciated.

Thank you in advance.
-------------------------------------------------------------------


Stephane Richard
"Ada World" webmaster
http://www.adaworld.com





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14 15:16       ` Stephane Richard
@ 2003-10-14 20:16         ` Jeffrey Carter
  0 siblings, 0 replies; 27+ messages in thread
From: Jeffrey Carter @ 2003-10-14 20:16 UTC (permalink / raw)


Stephane Richard wrote:

> Have a look atmy website (http://www.adaworld.com) I have a learning center
> that has tutorials AND freely available books in the PDF file format that
> might be of interest to you.

There are also a number of on-line books avaiable from www.adapower.com. 
Coming from C++, /Ada Distilled/ might be a good choice.

-- 
Jeff Carter
"Death awaits you all, with nasty, big, pointy teeth!"
Monty Python & the Holy Grail
20




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-14 12:39   ` Bleakcabal
  2003-10-14 12:57     ` sk
  2003-10-14 14:14     ` Problems with Ada.Text_IO and strings. (wants EOL character) Larry Kilgallen
@ 2003-10-14 20:19     ` Jeffrey Carter
  2 siblings, 0 replies; 27+ messages in thread
From: Jeffrey Carter @ 2003-10-14 20:19 UTC (permalink / raw)


Bleakcabal wrote:

> It would have been great if Ada strings read the end of line character
> and Put took care of them so the lines were automagicly sliced like C
> strings.

IIRC, C doesn't treat arrays of charany differently than any other 
array. There is, however, a large library in C that treats a NUL in an 
array of char as an EOS character.

Ada and C take 2 different, but consistent and workable, approaches to 
strings. You can certainly write a library in Ada to deal with strings 
the way C does. But this higher level of abstraction already exists in 
Ada as Ada.Strings.[Un]Bounded.

-- 
Jeff Carter
"Death awaits you all, with nasty, big, pointy teeth!"
Monty Python & the Holy Grail
20




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
                   ` (3 preceding siblings ...)
  2003-10-14 12:33 ` Bleakcabal
@ 2003-10-15  6:25 ` CheGueVerra
  2003-10-15 14:41   ` Martin Krischik
                     ` (2 more replies)
  4 siblings, 3 replies; 27+ messages in thread
From: CheGueVerra @ 2003-10-15  6:25 UTC (permalink / raw)


It's still not clear for me how to make sure, that when I have a String
let's say:

S1 : String(1..10);

How can I make sure that the String that I will receive and treat will be of
length 10 or less..

I'm still a little n00b

CheGueVerra







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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15  6:25 ` CheGueVerra
@ 2003-10-15 14:41   ` Martin Krischik
  2003-10-15 19:50     ` CheGueVerra
  2003-10-15 17:23   ` Marius Amado Alves
  2003-10-16  2:29   ` Steve
  2 siblings, 1 reply; 27+ messages in thread
From: Martin Krischik @ 2003-10-15 14:41 UTC (permalink / raw)


CheGueVerra wrote:

> It's still not clear for me how to make sure, that when I have a String
> let's say:
> 
> S1 : String(1..10);
> 
> How can I make sure that the String that I will receive and treat will be
> of length 10 or less..
> 
> I'm still a little n00b

Use Ada.Strings.Unbounded.Unbounded_String.

as for reading a line of text see:

http://adacl.sourceforge.net/html/______Include__AdaCL-Strings-IO__ads.htm
http://adacl.sourceforge.net/html/______Include__AdaCL-Strings-IO__adb.htm

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15  6:25 ` CheGueVerra
  2003-10-15 14:41   ` Martin Krischik
@ 2003-10-15 17:23   ` Marius Amado Alves
  2003-10-16  2:29   ` Steve
  2 siblings, 0 replies; 27+ messages in thread
From: Marius Amado Alves @ 2003-10-15 17:23 UTC (permalink / raw)
  To: comp.lang.ada

On Wed, 2003-10-15 at 06:25, CheGueVerra wrote:
> It's still not clear for me how to make sure, that when I have a String
> let's say:
> 
> S1 : String(1..10);
> 
> How can I make sure that the String that I will receive and treat will be of
> length 10 or less..

S1 has length 10, period. It is an array. How you do what you want
depends of your end of data condition. If it is end of line use
Get_Line. If it is end of file use a loop testing for End_Of_File. Etc.
In any case record how many characters were read and then use the slice
S1 (1 .. How_Many).




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15 14:41   ` Martin Krischik
@ 2003-10-15 19:50     ` CheGueVerra
  2003-10-15 22:00       ` Ludovic Brenta
  2003-10-15 23:39       ` Chad R. Meiners
  0 siblings, 2 replies; 27+ messages in thread
From: CheGueVerra @ 2003-10-15 19:50 UTC (permalink / raw)


While I've bookmarked the link you posted, and am very thankfull, the thing
is I don't believe I can use external libs for the homework, But at the same
time if I have a String variable that is declared (1..20)  Well, I don't
wnat the user to insert all blank spaces after his(her) name and not one
more because it will enter the buffer of the other string !!  I want to
understand, but I guess I'm seeing things to C or C++ like still, and have
to reteach my brain how to think, but it's hard to grasp everything when
your just entering Names and addresses

CheGueVerra





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15 19:50     ` CheGueVerra
@ 2003-10-15 22:00       ` Ludovic Brenta
  2003-10-16  1:19         ` Jeffrey Carter
  2003-10-15 23:39       ` Chad R. Meiners
  1 sibling, 1 reply; 27+ messages in thread
From: Ludovic Brenta @ 2003-10-15 22:00 UTC (permalink / raw)


"CheGueVerra" <chegueverra@hotmail.com> writes:

> While I've bookmarked the link you posted, and am very thankfull, the thing
> is I don't believe I can use external libs for the homework, But at the same
> time if I have a String variable that is declared (1..20)  Well, I don't
> wnat the user to insert all blank spaces after his(her) name and not one
> more because it will enter the buffer of the other string !!  I want to
> understand, but I guess I'm seeing things to C or C++ like still, and have
> to reteach my brain how to think, but it's hard to grasp everything when
> your just entering Names and addresses
> 
> CheGueVerra

I'm not going to post working code and do your homework for you, but
here are hints you may find useful.

Hint 1:

declare
   S : String (1 .. 20);
   Last : Natural;
begin
   Ada.Text_IO.Get_Line (S, Last); -- User's input is S (S'First .. Last)
end;

Hint 2: if Last = S'Last, then you will want to call
Ada.Text_IO.Get_Line again to read the rest of the input, and
concatenate this to S.

Hint 3: recursive calls are your friends.

HTH

-- 
Ludovic Brenta.



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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15 19:50     ` CheGueVerra
  2003-10-15 22:00       ` Ludovic Brenta
@ 2003-10-15 23:39       ` Chad R. Meiners
  2003-10-19  7:36         ` Martin Krischik
  1 sibling, 1 reply; 27+ messages in thread
From: Chad R. Meiners @ 2003-10-15 23:39 UTC (permalink / raw)


Try reading

http://www.adapower.com/lang/get_line.html

-CRM

"CheGueVerra" <chegueverra@hotmail.com> wrote in message
news:ryhjb.7736$PM2.813813@news20.bellglobal.com...
> While I've bookmarked the link you posted, and am very thankfull, the
thing
> is I don't believe I can use external libs for the homework, But at the
same
> time if I have a String variable that is declared (1..20)  Well, I don't
> wnat the user to insert all blank spaces after his(her) name and not one
> more because it will enter the buffer of the other string !!  I want to
> understand, but I guess I'm seeing things to C or C++ like still, and have
> to reteach my brain how to think, but it's hard to grasp everything when
> your just entering Names and addresses
>
> CheGueVerra
>
>





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15 22:00       ` Ludovic Brenta
@ 2003-10-16  1:19         ` Jeffrey Carter
  0 siblings, 0 replies; 27+ messages in thread
From: Jeffrey Carter @ 2003-10-16  1:19 UTC (permalink / raw)


Ludovic Brenta wrote:

> I'm not going to post working code and do your homework for you, but
> here are hints you may find useful.
> 
> Hint 1:
> 
> declare
>    S : String (1 .. 20);
>    Last : Natural;
> begin
>    Ada.Text_IO.Get_Line (S, Last); -- User's input is S (S'First .. Last)
> end;
> 
> Hint 2: if Last = S'Last, then you will want to call
> Ada.Text_IO.Get_Line again to read the rest of the input, and
> concatenate this to S.
> 
> Hint 3: recursive calls are your friends.

I don't think this is what the OP is looking for (a Get_Line function).

One way to deal with this is to read into a really long String using 
Get_Line, then use Ada.Strings.Fixed.Move to transfer the value to the 
Name variable. It's unlikely that a user will type in 100 characters for 
a name; 1000 practically guarantees that Get_Line will get the entire line.

Another is to write Get_Line yourself, not because you need to, but 
because it helps you understand what Get_Line is doing:

procedure Get_Line (Item : out String; Last : out Natural) is
begin
    Last := Item'First - 1;

    Read : for I in Item'range loop
       if End_Of_Line then
          Skip_Line;

          return;
       end if;

       Get (Item (I) );
       Last := I;
    end loop Read;
end Get_Line;

The important point is that Skip_Line is only called inside the loop. 
The loop exits when Item is completely filled; Skip_Line is not called. 
If the loop exits, Last = Item'Last. Thus, if on return from Get_Line, 
Last = Item'Last, then there is at least an unprocessed line terminator 
in the buffer, possibly preceded by some unprocessed characters. You can 
get rid of these by calling Skip_Line yourself.

You can also write a procedure to deal with all of this:

procedure Get_Whole_Line (Item : out String; Last : out Natural) is
begin
    Get_Line (Item, Last);

    if Last = Item'Last then
       Skip_Line;
    end if;
end Get_Whole_Line;

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15  6:25 ` CheGueVerra
  2003-10-15 14:41   ` Martin Krischik
  2003-10-15 17:23   ` Marius Amado Alves
@ 2003-10-16  2:29   ` Steve
  2003-10-16  5:51     ` CheGueVerra
  2 siblings, 1 reply; 27+ messages in thread
From: Steve @ 2003-10-16  2:29 UTC (permalink / raw)


In Ada when you create a string, say:

  S1 : String(1..10)

and pass that string to a procedure, say

  Get_Line( S1, last );

Where Get_Line has the interface defined as:

  procedure Get_Line( str : out String; last : out Natural );

You'll notice that the length of the string is not explicity passed as a
separate item to the procedure.  In Ada the bounds of the array are passed
implicitly.

The Get_Line procedure may use the attributes 'First and 'Last (written
str'First and str'Last respectivly) to obtain the first and last indexes of
the string passed in.  So the implementation of Get_Line can limit the
number of characters read to the size of the string passed in and return the
actual number of characters read in "last".

So in answer to your question the "Get_Line" procedure will make sure that
the string you receive is a length of 10 or less.

Steve
(The Duck)


"CheGueVerra" <chegueverra@hotmail.com> wrote in message
news:mL5jb.3237$Z_2.269599@news20.bellglobal.com...
> It's still not clear for me how to make sure, that when I have a String
> let's say:
>
> S1 : String(1..10);
>
> How can I make sure that the String that I will receive and treat will be
of
> length 10 or less..
>
> I'm still a little n00b
>
> CheGueVerra
>
>
>
>





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-16  2:29   ` Steve
@ 2003-10-16  5:51     ` CheGueVerra
  2003-10-16  9:51       ` CheGueVerra
  0 siblings, 1 reply; 27+ messages in thread
From: CheGueVerra @ 2003-10-16  5:51 UTC (permalink / raw)


Well I'm going to start a small project to isolat the behavior I want and
save it for eternity  ;)

Thanks for all your help, keep you posted how the String Project goes.

But my brain is slowly adjusting to ada.. to slowly for my taste but getting
there
BTW, the homework is not about strings, but a Pizza Delivery system, to
teach us how to use ads and adb files. It's not said that I have to handle
the strings in any way but I wanted it to be at least Teacher friendly :)

CheGueVerra







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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-16  5:51     ` CheGueVerra
@ 2003-10-16  9:51       ` CheGueVerra
  0 siblings, 0 replies; 27+ messages in thread
From: CheGueVerra @ 2003-10-16  9:51 UTC (permalink / raw)


Thanks for all your help to make it easier for me to understand Strings in
ada, but I'm sure that it was just the tip of the String Iceberg

CheGueVerra





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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-15 23:39       ` Chad R. Meiners
@ 2003-10-19  7:36         ` Martin Krischik
  2003-10-19 19:24           ` Chad R. Meiners
  0 siblings, 1 reply; 27+ messages in thread
From: Martin Krischik @ 2003-10-19  7:36 UTC (permalink / raw)


Chad R. Meiners wrote:

> Try reading
> 
> http://www.adapower.com/lang/get_line.html

If you read my link you would know that the get_line on adapower is buggy.
It may produce a premature end of file exception when reeading the last
line.

See AdaCL for a corrected version.
 
With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: Problems with Ada.Text_IO and strings.
  2003-10-19  7:36         ` Martin Krischik
@ 2003-10-19 19:24           ` Chad R. Meiners
  0 siblings, 0 replies; 27+ messages in thread
From: Chad R. Meiners @ 2003-10-19 19:24 UTC (permalink / raw)



"Martin Krischik" <krischik@users.sourceforge.net> wrote in message
news:2042269.sOFEFQ0riL@linux1.krischik.com...
> Chad R. Meiners wrote:
>
> > Try reading
> >
> > http://www.adapower.com/lang/get_line.html
>
> If you read my link you would know that the get_line on adapower is buggy.
> It may produce a premature end of file exception when reeading the last
> line.

That being said, the link I provided explains how Get_Line works, which is
what I wanted the OP to read.

-CRM





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

end of thread, other threads:[~2003-10-19 19:24 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
2003-10-13 23:51 ` Larry Hazel
2003-10-14  0:06 ` Chad R. Meiners
2003-10-14 12:32   ` Bleakcabal
2003-10-14 15:03     ` Robert I. Eachus
2003-10-14 15:16       ` Stephane Richard
2003-10-14 20:16         ` Jeffrey Carter
2003-10-14  1:29 ` Jeffrey Carter
2003-10-14 12:39   ` Bleakcabal
2003-10-14 12:57     ` sk
2003-10-14 14:14     ` Problems with Ada.Text_IO and strings. (wants EOL character) Larry Kilgallen
2003-10-14 16:20       ` Stephen Leake
2003-10-14 16:45         ` Stephane Richard
2003-10-14 20:19     ` Problems with Ada.Text_IO and strings Jeffrey Carter
2003-10-14 12:33 ` Bleakcabal
2003-10-15  6:25 ` CheGueVerra
2003-10-15 14:41   ` Martin Krischik
2003-10-15 19:50     ` CheGueVerra
2003-10-15 22:00       ` Ludovic Brenta
2003-10-16  1:19         ` Jeffrey Carter
2003-10-15 23:39       ` Chad R. Meiners
2003-10-19  7:36         ` Martin Krischik
2003-10-19 19:24           ` Chad R. Meiners
2003-10-15 17:23   ` Marius Amado Alves
2003-10-16  2:29   ` Steve
2003-10-16  5:51     ` CheGueVerra
2003-10-16  9:51       ` CheGueVerra

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