comp.lang.ada
 help / color / mirror / Atom feed
* Please help a beginner! (second try)
@ 1996-02-17  0:00 Ryan
  1996-02-17  0:00 ` smoneill
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ryan @ 1996-02-17  0:00 UTC (permalink / raw)



Could somebody please help me with what I am sure is a simple
problem. I need to do some basic text and integer IO. Apparently
I need the TEXT_IO library for this - it is on my system, and
I believe I am using it in the following code. However, when I 
compile the program, the errors mentioned in the comments are
given out :

with TEXT_IO;
package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);
with INT_IO;

procedure proc1 is
begin
  TEXT_IO.PUT("Hello");
-- fails ... "TEXT_IO not defined"
  INT_IO.PUT(2);         
-- accepted
end proc1;

procedure main is
begin
  TEXT_IO.PUT("Hello");
-- fails ... "TEXT_IO not defined"
  INT_IO.PUT(2);         
-- fails ... "INT_IO not defined"
end main;


The system is a DEC, the compiler is just called 'ada', and it's ada83,
not ada95, and that's about all I know. The errors refer me to section 8.3
of the LRM, but that doesn't help!

I am fairly experienced in C/C++, Pascal, etc, and have fiddled with
Ada a few years ago, but don't have access to any of that source code to
work from.

Please help! In this newsgroup, or by email to kst1ju@herts.ac.uk

	Thanks,

	Nigel.  




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

* Re: Please help a beginner! (second try)
  1996-02-17  0:00 Please help a beginner! (second try) Ryan
@ 1996-02-17  0:00 ` smoneill
  1996-02-17  0:00   ` Robert Dewar
  1996-02-18  0:00 ` Steve Doiel
  1996-02-19  0:00 ` Arthur Evans Jr
  2 siblings, 1 reply; 6+ messages in thread
From: smoneill @ 1996-02-17  0:00 UTC (permalink / raw)


kst1ju@herts.ac.uk (Ryan) wrote:
>Could somebody please help me with what I am sure is a simple
>problem. I need to do some basic text and integer IO.

The problem is simple.  While you probably have all of the following
in a single file it is actually 3 compilation units (the instantiation
of Integer_IO, proc1 and main).  To gain visibility to the services
provided by Text_IO each of these units must import (with) the Text_IO
package.  IOW, proc1 and main must each 'with' Text_IO and Int_IO.

>with TEXT_IO;
>package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);

--end of compilation unit INT_IO

with Text_IO;
>with INT_IO;
>procedure proc1 is
>begin
>  TEXT_IO.PUT("Hello");
>  INT_IO.PUT(2);         
>end proc1;

-- end of compilation unit proc1

with Text_IO;
with Int_IO;
>procedure main is
>begin
>  TEXT_IO.PUT("Hello");
>  INT_IO.PUT(2);         
>end main;


This will noew work fine.  BTW, DEC Ada supplies a version of Integer_IO
pre-instantiated for you.  It's called Integer_Text_IO.  Nothing special
about it, it just saves you from instantiating Integer_IO.  Just remove
the instantiation of INT_IO and change the references to Integer_Text_IO.

>	Thanks,

Glad to be of help.

-- 
Steve O'Neill                      | "No,no,no, don't tug on that!
Sanders, A Lockheed Martin Company |  You never know what it might
smoneill@sanders.lockheed.com      |  be attached to." 
(603) 885-8774  fax: (603) 885-4071|    Buckaroo Banzai






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

* Re: Please help a beginner! (second try)
  1996-02-17  0:00 ` smoneill
@ 1996-02-17  0:00   ` Robert Dewar
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1996-02-17  0:00 UTC (permalink / raw)


Steve says

"This will noew work fine.  BTW, DEC Ada supplies a version of Integer_IO
pre-instantiated for you.  It's called Integer_Text_IO.  Nothing special
about it, it just saves you from instantiating Integer_IO.  Just remove
the instantiation of INT_IO and change the references to Integer_Text_IO."

Here Steve is making a comment about Ada 83. In Ada 95, the package
Integer_Text_IO is a standard part of the language, available in any
Ada 95 implementation (and of course fully supported in GNAT).





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

* Re: Please help a beginner! (second try)
  1996-02-17  0:00 Please help a beginner! (second try) Ryan
  1996-02-17  0:00 ` smoneill
@ 1996-02-18  0:00 ` Steve Doiel
  1996-02-18  0:00   ` Robert Dewar
  1996-02-19  0:00 ` Arthur Evans Jr
  2 siblings, 1 reply; 6+ messages in thread
From: Steve Doiel @ 1996-02-18  0:00 UTC (permalink / raw)


In <4g517q$438@helios.herts.ac.uk>, kst1ju@herts.ac.uk (Ryan) writes:
>
>Could somebody please help me with what I am sure is a simple
>problem. I need to do some basic text and integer IO. Apparently
>I need the TEXT_IO library for this - it is on my system, and
>I believe I am using it in the following code. However, when I 
>compile the program, the errors mentioned in the comments are
>given out :
>
>with TEXT_IO;
>package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);
>with INT_IO;
>
>procedure proc1 is
>begin
>  TEXT_IO.PUT("Hello");
>-- fails ... "TEXT_IO not defined"
>  INT_IO.PUT(2);         
>-- accepted
>end proc1;
>
>procedure main is
>begin
>  TEXT_IO.PUT("Hello");
>-- fails ... "TEXT_IO not defined"
>  INT_IO.PUT(2);         
>-- fails ... "INT_IO not defined"
>end main;
>
>
[snip]
>	Thanks,
>
>	Nigel. 

I too come from a Pascal background and have shifted toward Ada.  I did
some slight re-arranging of your code, and the following compiles and runs
under GNAT 3.01 on OS/2.  It may not be exactly what you wanted, but it
gives the output: Hello 2

with TEXT_IO;

procedure main is

package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);

procedure proc1 is
begin
  TEXT_IO.PUT("Hello");
  INT_IO.PUT(2);         
end proc1;

begin
  TEXT_IO.PUT("Hello");
  INT_IO.PUT(2);         
end main;

In simple terms, the main program in Pascal is something like:

PROGRAM Main( input, output );

BEGIN
END.

Well, in Ada the main program is a procedure:

PROCEDURE Main IS

BEGIN
END Main;

All the WITH's go before the main procedure heading.

Hope this helps!
Steve Doiel





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

* Re: Please help a beginner! (second try)
  1996-02-18  0:00 ` Steve Doiel
@ 1996-02-18  0:00   ` Robert Dewar
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1996-02-18  0:00 UTC (permalink / raw)


Steve said

"Well, in Ada the main program is a procedure:

 PROCEDURE Main IS
 
 BEGIN
 END Main;
 
Careful Steve, this is wrong, you cannot have compiled it, you meant

 procedure Main is
 begin
    null;
 end Main;
Also, Steve's final solution for the procedure:

  with TEXT_IO;
  
  procedure main is
  
  package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);
  
  procedure proc1 is
  begin
    TEXT_IO.PUT("Hello");
    INT_IO.PUT(2);
  end proc1;
  
  begin
    TEXT_IO.PUT("Hello");
    INT_IO.PUT(2);
  end main;

is very confused, it seems to be the result of random code rearrangement
until something "works". In the above, which is not properly indented,
no doubt adding to the confusion, you have an internal procedure proc1
which is never called. A corrected version of the above is:

  with Ada.Text_IO;
  procedure Main is
  
    package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
  
  begin
    Ada.Text_IO.Put ("Hello");
    Int_IO.Put (2);
  end Main;

However, I prefer not to teach generic instantiation so early (I would
bet for example that Steve does not 100% understand what he wrote there :-)
Instead in Ada 95, I would teach:

  with Ada.Text_IO;
  with Ada.Integer_Text_IO;
  
  procedure Main is
  begin
    Ada.Text_IO.Put ("Hello");
    Ada.Integer_Text_IO.Put (2);
  end Main;

Of course this only works with the built-in type Integer, but one of the
few legitimate uses of the built-in types is in teaching right at the
beginning. I also changed the casing to be more "standard". The use of
all upper case identifiers is now largely out of favor, so you might as
well teach the above casing style from the start.

Another approach is to introduce the attribute functions early on:

  with Ada.Text_IO;
  procedure Main is
  begin
    Ada.Text_IO.Put ("Hello");
    Ada.Text_IO.Put (Integer'Image (2));
  end Main;

One general comment is that if you are trying to understand Ada, or any
other language, at an elementary level like this, get a book and read it!
Using a newsgroup to try to learn at this level is a dicey proposition,
because sure there are people who can answer correctly, but the information
you get posted on any newsgroup is quite unreliable. On CLA for instance,
while it is better than some other newsgroups, people routinely through
accident or ignorance post as gospel statements about Ada that are
completely wrong, and if you don't know a fair amount to start with,
you can easily get confused.

To those who post suggested solutions to anything, a plea: PLEASE be sure
to compile and test your solution. Even if you know Ada well, it is good
insurance against a simple slip-up which can cause great confusion. For
example, I compiled and ran all three examples above, they are very simple,
and did indeed run error-free with no surprises, but no one can be 100%
sure of not making silly mistakes :-)





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

* Re: Please help a beginner! (second try)
  1996-02-17  0:00 Please help a beginner! (second try) Ryan
  1996-02-17  0:00 ` smoneill
  1996-02-18  0:00 ` Steve Doiel
@ 1996-02-19  0:00 ` Arthur Evans Jr
  2 siblings, 0 replies; 6+ messages in thread
From: Arthur Evans Jr @ 1996-02-19  0:00 UTC (permalink / raw)


In article <4g517q$438@helios.herts.ac.uk>, kst1ju@herts.ac.uk (Ryan) wrote:

> Could somebody please help me with what I am sure is a simple
> problem.
  [snip]
> 
> with TEXT_IO;
> package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);
> with INT_IO;
> 
> procedure proc1 is
> begin
>   TEXT_IO.PUT("Hello");
> -- fails ... "TEXT_IO not defined"
>   INT_IO.PUT(2);         
> -- accepted
> end proc1;
> 
> procedure main is
> begin
>   TEXT_IO.PUT("Hello");
> -- fails ... "TEXT_IO not defined"
>   INT_IO.PUT(2);         
> -- fails ... "INT_IO not defined"
> end main;

We are taught to use white space, including vertical spacing, to make
programs easier to read.  Here's an interesting example where the use of
white space has obscured what is actually happening.  An Ada compiler
interprets the above like this, where the only changes I have made are
to layout:

    --------------------------------------------

    -- 1st compliation unit
    with TEXT_IO;
    package INT_IO is new TEXT_IO.INTEGER_IO(INTEGER);

    --------------------------------------------

    -- 2nd compilation unit
    with INT_IO;
    procedure proc1 is
    begin
      TEXT_IO.PUT("Hello");
    -- fails ... "TEXT_IO not defined"
      INT_IO.PUT(2);         
    -- accepted
    end proc1;

    --------------------------------------------

    -- 3rd compilation unit
    procedure main is
    begin
      TEXT_IO.PUT("Hello");
    -- fails ... "TEXT_IO not defined"
      INT_IO.PUT(2);         
    -- fails ... "INT_IO not defined"
    end main;

Now it's clear that "with TEXT_IO" applies to only the instantiation of
TEXT_IO.INTEGER_IO and so TEXT_IO is not visible in the 2nd and 3rd
units.  Further, "WITH INT_IO" apllies to only the 2nd unit.  Add some
"with" clauses and all should be well.

Art Evans

Arthur Evans Jr, PhD        Phone: 412-963-0839
Ada Consulting              FAX:   412-963-0927
461 Fairview Road
Pittsburgh PA  15238-1933
evans@evans.pgh.pa.us




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

end of thread, other threads:[~1996-02-19  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-02-17  0:00 Please help a beginner! (second try) Ryan
1996-02-17  0:00 ` smoneill
1996-02-17  0:00   ` Robert Dewar
1996-02-18  0:00 ` Steve Doiel
1996-02-18  0:00   ` Robert Dewar
1996-02-19  0:00 ` Arthur Evans Jr

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