comp.lang.ada
 help / color / mirror / Atom feed
* Simple call of Procedure in package from a main program
@ 2015-10-31 12:53 comicfanzine
  2015-10-31 15:52 ` mockturtle
  2015-10-31 17:54 ` Jeffrey R. Carter
  0 siblings, 2 replies; 5+ messages in thread
From: comicfanzine @ 2015-10-31 12:53 UTC (permalink / raw)


Hello everyone ,

I have a simple main program , who call a procedure in a package but it not compile . In total , i have 3 files(1 main program , 1 packagefile.adb , and , 1 packagefile.ads) :

user_or_admin.adb(main program) :

[code]with ada.text_io ;    use ada.text_io ;
with identification;  use identification;

Procedure User_or_admin is
      
  begin

New_Line ; 

     Put_line("Hello user, what's your name");
	   
	   yes_or_no;
	   
        if Password = "admin"
            then Put_Line("Welcome administrator");
end if;                       end User_or_admin ;
[/code=ada]

identification.adb(packagefile)

[code]with ada.text_io ;    use ada.text_io ;

package identification is

Procedure yes_or_no is
      
     Password :  String(1..5) ;

Begin

       declare 
               
    name : String := ada.text_io.get_line ; 

         begin
	   if name = "fanzine" 
           then Put("what is the password : ") ;
            Get(Password) ; New_Line ; Skip_line  ;

            elsif name /= "fanzine"
             then Put_line("You're not administrator") ; 
	     
      end if;

    end ;
    
end yes_or_no;

end identification;[/code=ada]

identification.ads(packagefile)

[code]package identification is

   Procedure yes_or_no (Password :  String );
   
end identification;[/code=ada]

If you stil don't understand what i want to do after reading the files , i explain :

In the main program , after Put_line i want to have the procedure in the packagefile be executed , so after the Hello[...] : do the get_line which is in the packagefile and do the "if-then" condition after it , and finaly if this "if-then" condition is valid , do the Password condition . 

Don't hesitate to correct the files if you think what i want to do correspond what you doing . 

Thanks for you time , it is always a pleasure to code in Ada and have the help of others ada programers . 

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

* Re: Simple call of Procedure in package from a main program
  2015-10-31 12:53 Simple call of Procedure in package from a main program comicfanzine
@ 2015-10-31 15:52 ` mockturtle
  2015-10-31 17:32   ` comicfanzine
  2015-10-31 17:54 ` Jeffrey R. Carter
  1 sibling, 1 reply; 5+ messages in thread
From: mockturtle @ 2015-10-31 15:52 UTC (permalink / raw)


I see two syntax problems: first, in identification.adb "body" is missing (is "package body Identification is..." not "package Identification is..."). Moreover, Password is declared inside yes_or_no, so it is not visible to the main program, actually, when the control returns to the main Password does not exist anymore (a local variable like Password is created when the procedure is called and destroyed when the procedure finishes).

In order to make it work, you should move Password in a place visible to both the main and Identification, that is, in the spec file identification.ads.  It is not the best style, though; maybe a better solution would be to have yes_or_no returning the read password.

A final suggestion: when asking for help for a program that does not compile, add also the error messages you get.  It will help a lot other people helping you.


Riccardo

On Saturday, October 31, 2015 at 1:53:33 PM UTC+1, comicf...@gmail.com wrote:
> Hello everyone ,
> 
> I have a simple main program , who call a procedure in a package but it not compile . In total , i have 3 files(1 main program , 1 packagefile.adb , and , 1 packagefile.ads) :
> 
> user_or_admin.adb(main program) :
> 
> [code]with ada.text_io ;    use ada.text_io ;
> with identification;  use identification;
> 
> Procedure User_or_admin is
>       
>   begin
> 
> New_Line ; 
> 
>      Put_line("Hello user, what's your name");
> 	   
> 	   yes_or_no;
> 	   
>         if Password = "admin"
>             then Put_Line("Welcome administrator");
> end if;                       end User_or_admin ;
> [/code=ada]
> 
> identification.adb(packagefile)
> 
> [code]with ada.text_io ;    use ada.text_io ;
> 
> package identification is
> 
> Procedure yes_or_no is
>       
>      Password :  String(1..5) ;
> 
> Begin
> 
>        declare 
>                
>     name : String := ada.text_io.get_line ; 
> 
>          begin
> 	   if name = "fanzine" 
>            then Put("what is the password : ") ;
>             Get(Password) ; New_Line ; Skip_line  ;
> 
>             elsif name /= "fanzine"
>              then Put_line("You're not administrator") ; 
> 	     
>       end if;
> 
>     end ;
>     
> end yes_or_no;
> 
> end identification;[/code=ada]
> 
> identification.ads(packagefile)
> 
> [code]package identification is
> 
>    Procedure yes_or_no (Password :  String );
>    
> end identification;[/code=ada]
> 
> If you stil don't understand what i want to do after reading the files , i explain :
> 
> In the main program , after Put_line i want to have the procedure in the packagefile be executed , so after the Hello[...] : do the get_line which is in the packagefile and do the "if-then" condition after it , and finaly if this "if-then" condition is valid , do the Password condition . 
> 
> Don't hesitate to correct the files if you think what i want to do correspond what you doing . 
> 
> Thanks for you time , it is always a pleasure to code in Ada and have the help of others ada programers .

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

* Re: Simple call of Procedure in package from a main program
  2015-10-31 15:52 ` mockturtle
@ 2015-10-31 17:32   ` comicfanzine
  0 siblings, 0 replies; 5+ messages in thread
From: comicfanzine @ 2015-10-31 17:32 UTC (permalink / raw)


Le samedi 31 octobre 2015 16:52:30 UTC+1, mockturtle a écrit :
> I see two syntax problems: first, in identification.adb "body" is missing (is "package body Identification is..." not "package Identification is..."). Moreover, Password is declared inside yes_or_no, so it is not visible to the main program, actually, when the control returns to the main Password does not exist anymore (a local variable like Password is created when the procedure is called and destroyed when the procedure finishes).
> 
> In order to make it work, you should move Password in a place visible to both the main and Identification, that is, in the spec file identification.ads.  It is not the best style, though; maybe a better solution would be to have yes_or_no returning the read password.
> 
> A final suggestion: when asking for help for a program that does not compile, add also the error messages you get.  It will help a lot other people helping you.
> 
> 
> Riccardo
> 
> On Saturday, October 31, 2015 at 1:53:33 PM UTC+1, comicf...@gmail.com wrote:
> > Hello everyone ,
> > 
> > I have a simple main program , who call a procedure in a package but it not compile . In total , i have 3 files(1 main program , 1 packagefile.adb , and , 1 packagefile.ads) :
> > 
> > user_or_admin.adb(main program) :
> > 
> > [code]with ada.text_io ;    use ada.text_io ;
> > with identification;  use identification;
> > 
> > Procedure User_or_admin is
> >       
> >   begin
> > 
> > New_Line ; 
> > 
> >      Put_line("Hello user, what's your name");
> > 	   
> > 	   yes_or_no;
> > 	   
> >         if Password = "admin"
> >             then Put_Line("Welcome administrator");
> > end if;                       end User_or_admin ;
> > [/code=ada]
> > 
> > identification.adb(packagefile)
> > 
> > [code]with ada.text_io ;    use ada.text_io ;
> > 
> > package identification is
> > 
> > Procedure yes_or_no is
> >       
> >      Password :  String(1..5) ;
> > 
> > Begin
> > 
> >        declare 
> >                
> >     name : String := ada.text_io.get_line ; 
> > 
> >          begin
> > 	   if name = "fanzine" 
> >            then Put("what is the password : ") ;
> >             Get(Password) ; New_Line ; Skip_line  ;
> > 
> >             elsif name /= "fanzine"
> >              then Put_line("You're not administrator") ; 
> > 	     
> >       end if;
> > 
> >     end ;
> >     
> > end yes_or_no;
> > 
> > end identification;[/code=ada]
> > 
> > identification.ads(packagefile)
> > 
> > [code]package identification is
> > 
> >    Procedure yes_or_no (Password :  String );
> >    
> > end identification;[/code=ada]
> > 
> > If you stil don't understand what i want to do after reading the files , i explain :
> > 
> > In the main program , after Put_line i want to have the procedure in the packagefile be executed , so after the Hello[...] : do the get_line which is in the packagefile and do the "if-then" condition after it , and finaly if this "if-then" condition is valid , do the Password condition . 
> > 
> > Don't hesitate to correct the files if you think what i want to do correspond what you doing . 
> > 
> > Thanks for you time , it is always a pleasure to code in Ada and have the help of others ada programers .



Le samedi 31 octobre 2015 16:52:30 UTC+1, mockturtle a écrit :
> I see two syntax problems: first, in identification.adb "body" is missing (is "package body Identification is..." not "package Identification is..."). Moreover, Password is declared inside yes_or_no, so it is not visible to the main program, actually, when the control returns to the main Password does not exist anymore (a local variable like Password is created when the procedure is called and destroyed when the procedure finishes).
> 
> In order to make it work, you should move Password in a place visible to both the main and Identification, that is, in the spec file identification.ads.  It is not the best style, though; maybe a better solution would be to have yes_or_no returning the read password.
> 
> A final suggestion: when asking for help for a program that does not compile, add also the error messages you get.  It will help a lot other people helping you.
> 
> 
> Riccardo
> 

Thanks , i have put "body" like you said for the first syntax problem and i understand that Password is invisible to the main program , but i don't understand your second point about moving Password , can you be more specific or(and) explain me with the code ? 

Also here is the 2 errors:

user_or_admin.adb:12:12: missing argument for parameter "Password" in call to "Yes_Or_No" declared at identification.ads:3
user_or_admin.adb:14:12: "Password" is undefined


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

* Re: Simple call of Procedure in package from a main program
  2015-10-31 12:53 Simple call of Procedure in package from a main program comicfanzine
  2015-10-31 15:52 ` mockturtle
@ 2015-10-31 17:54 ` Jeffrey R. Carter
  2015-11-01 10:50   ` comicfanzine
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2015-10-31 17:54 UTC (permalink / raw)


On 10/31/2015 05:53 AM, comicfanzine@gmail.com wrote:
> 
> [code]with ada.text_io ;    use ada.text_io ;
> with identification;  use identification;
> 
> Procedure User_or_admin is
>       
>   begin
> 
> New_Line ; 
> 
>      Put_line("Hello user, what's your name");
> 	   
> 	   yes_or_no;

Identification.Yes_Or_No has a mode-in parameter, Password, without a default,
and you do not supply it.

> 	   
>         if Password = "admin"

There is nothing named Password visible here.

>             then Put_Line("Welcome administrator");
> end if;                       end User_or_admin ;
> [/code=ada]
> 
> [code]with ada.text_io ;    use ada.text_io ;
> 
> package identification is

This appears to be a package body, but the reserved word "body" is missing after
"package".

> 
> Procedure yes_or_no is
>       
>      Password :  String(1..5) ;
> 
> Begin
> 
>        declare 
>                
>     name : String := ada.text_io.get_line ; 
> 
>          begin
> 	   if name = "fanzine" 
>            then Put("what is the password : ") ;
>             Get(Password) ; New_Line ; Skip_line  ;
> 
>             elsif name /= "fanzine"

You know that Name /= "fanzine" if you get here.

>              then Put_line("You're not administrator") ; 
> 	     
>       end if;
> 
>     end ;
>     
> end yes_or_no;
> 
> end identification;[/code=ada]
> 
> [code]package identification is
> 
>    Procedure yes_or_no (Password :  String );

There is no body for this procedure declaration. The procedure body Yes_Or_No in
the package body has no parameters, so it is a different procedure from this one.

>    
> end identification;[/code=ada]

It seems to me you need something like

package Identification is
   subtype Password_Value is String (1 .. 5);

   procedure Get (Password : out Password_Value);
end Identification;

with Ada.Text_IO;
with Identification;

procedure Log_In is
   Password : Identification.Password_Value;
begin -- Log_In
   Identification.Get (Password => Password);

   if Password = "admin" then
      Ada.Text_IO.Put_Line (Item => "Welcome, Administrator.");
   end if;
end Log_In;

with Ada.Text_IO;

package body Identification is
   procedure Get (Password : out Password_Value) is
   begin -- Get
      Ada.Text_IO.Put (Item => "Hello, User. What's your name? ");

      Get_Name : declare
         Name : constant String := Ada.Text_IO.Get_Line;
      begin -- Get_Name
         if Name /= "fanzine" then
            Ada.Text_IO.Put_Line (Item => "You're not Administrator.");
            -- There is a 1 / 256 ** 5 chance that Password will contain "admin"
         else
            Ada.Text_IO.Get (Item => Password); -- Requires 5 characters
            Ada.Text_IO.Skip_Line;
            Ada.Text_IO.New_Line;
         end if;
      end Get_Name;
   end Get;
end Identification;

-- 
Jeff Carter
"Monsieur Arthur King, who has the brain of a duck, you know."
Monty Python & the Holy Grail
09


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

* Re: Simple call of Procedure in package from a main program
  2015-10-31 17:54 ` Jeffrey R. Carter
@ 2015-11-01 10:50   ` comicfanzine
  0 siblings, 0 replies; 5+ messages in thread
From: comicfanzine @ 2015-11-01 10:50 UTC (permalink / raw)


Le samedi 31 octobre 2015 18:54:05 UTC+1, Jeffrey R. Carter a écrit :

> It seems to me you need something like
> 
> package Identification is
>    subtype Password_Value is String (1 .. 5);
> 
>    procedure Get (Password : out Password_Value);
> end Identification;
> 
> with Ada.Text_IO;
> with Identification;
> 
> procedure Log_In is
>    Password : Identification.Password_Value;
> begin -- Log_In
>    Identification.Get (Password => Password);
> 
>    if Password = "admin" then
>       Ada.Text_IO.Put_Line (Item => "Welcome, Administrator.");
>    end if;
> end Log_In;
> 
> with Ada.Text_IO;
> 
> package body Identification is
>    procedure Get (Password : out Password_Value) is
>    begin -- Get
>       Ada.Text_IO.Put (Item => "Hello, User. What's your name? ");
> 
>       Get_Name : declare
>          Name : constant String := Ada.Text_IO.Get_Line;
>       begin -- Get_Name
>          if Name /= "fanzine" then
>             Ada.Text_IO.Put_Line (Item => "You're not Administrator.");
>             -- There is a 1 / 256 ** 5 chance that Password will contain "admin"
>          else
>             Ada.Text_IO.Get (Item => Password); -- Requires 5 characters
>             Ada.Text_IO.Skip_Line;
>             Ada.Text_IO.New_Line;
>          end if;
>       end Get_Name;
>    end Get;
> end Identification;

Thanks that exactly what i wanted to do :)

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

end of thread, other threads:[~2015-11-01 10:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-31 12:53 Simple call of Procedure in package from a main program comicfanzine
2015-10-31 15:52 ` mockturtle
2015-10-31 17:32   ` comicfanzine
2015-10-31 17:54 ` Jeffrey R. Carter
2015-11-01 10:50   ` comicfanzine

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