comp.lang.ada
 help / color / mirror / Atom feed
* Problem with Get_Line
@ 2002-09-26  5:35 Mauricio Tellez
  2002-09-27 19:51 ` Frank J. Lhota
  0 siblings, 1 reply; 4+ messages in thread
From: Mauricio Tellez @ 2002-09-26  5:35 UTC (permalink / raw)


Hi, I'm new to Ada but with some years of experience in C. I write a
small program to practice but with an result. The program is kind of
address book. The problem is that a call to Get_Line seems to not
execute. Why? As you can see in the code I'm still influenced by C so
any
comment about the code is also welcome. Thanks in advance. This is code:

with Ada.Text_Io, Ada.Strings, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Strings, Ada.Integer_Text_Io;

procedure Agenda is
   type Agenda_Record is
      record
         Nombre : String ( 1 .. 30 );
         Len : Integer;
         Usado : Character := 'N';
      end record;

   Max : constant Integer := 3;
   type Agenda_Type is array(1 .. Max) of Agenda_Record;
   Agenda : Agenda_Type;
   Opc : Integer;

   function Regresa_Libre (Agenda : in Agenda_Type) return Integer is
   begin
      for I in 1 .. Max loop
         if Agenda(I).Usado = 'N' then
            return I;
         end if;
      end loop;
      return 0;
   end Regresa_Libre;

   procedure Lee_Nombres (Agenda : out Agenda_Type) is
      Pos : Integer := 1;
   begin
      Pos := Regresa_Libre(Agenda);
      Flush;
      Put_Line("La posicion es: ");
      Put(Pos);
      if Pos = 0 then
         Put_Line("No hay espacio en la agenda");
      else
         Put_Line("Nombre: ");
-- This Get_Line do not execute!!!
         Get_Line(Agenda(Pos).nombre, Agenda(Pos).Len);
      end if;
   end Lee_Nombres;

begin
   loop
      Put_Line("** Agenda en Ada95 **");
      Put_Line("");
      Put_Line("1. Insertar registros a la agenda");
      Put_Line("9. Salir");
      Put_Line("");
      Put("Opcion> ");
      Get(Opc);
      exit when Opc = 9;
      case Opc is
         when 1 =>
            Lee_Nombres(Agenda);
when others =>
            Put_Line("Opcion Incorrecta!!!");
      end case;

   end loop;

   Lee_Nombres( Agenda );
   Put_Line("Los nombres leidos son:");
   for I in 1 .. Max loop
      Put_Line(Agenda(I).Nombre);
      Put(Agenda(I).Len);
   end loop;
end Agenda;

--
Mauricio T�llez Jim�nez
Facultad de Inform�tica UV
mtellez@xal.megared.net.mx

Programming today is a race between software engineers
striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots.
So far, the universe is winning.
                                         Richard Cook



^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: Problem with Get_Line
@ 2002-09-26  5:34 Grein, Christoph
  0 siblings, 0 replies; 4+ messages in thread
From: Grein, Christoph @ 2002-09-26  5:34 UTC (permalink / raw)


> with Ada.Text_Io, Ada.Strings, Ada.Integer_Text_Io;
> use  Ada.Text_Io, Ada.Strings, Ada.Integer_Text_Io;
> 
> procedure Agenda is
>    type Agenda_Record is
>       record
>          Nombre : String ( 1 .. 30 );
>          Len : Integer;
           Len : Natural := 0;               -- this is preferable
>          Usado : Character := 'N';
           Usado: Boolean := False;          -- this is preferable
>       end record;
> 
>    Max : constant Integer := 3;
     Max : constant := 3;                    -- this is preferable
     type Agenda_Index is range 0 .. Max;
     subtype Proper_Indes is Agenda_Index range 1 .. Agenda_Index'Last
>    type Agenda_Type is array(1 .. Max) of Agenda_Record;
     type Agenda_Type is array (Proper_Indes) of Agenda_Record;
>    Agenda : Agenda_Type;
>    Opc : Integer;
     Use an enumeration type for options like
     type Opcion is (Leer, Terminar);
> 
>    function Regresa_Libre (Agenda : in Agenda_Type) return Integer is
>    function Regresa_Libre (Agenda : in Agenda_Type) return Agenda_Index is
>    begin
>       for I in 1 .. Max loop
        for I in Agenda'Range loop           -- this is preferable
>          if Agenda(I).Usado = 'N' then
           if not Agenda(I).Usado then       -- this is preferable
>             return I;
>          end if;
>       end loop;
>       return 0;
>    end Regresa_Libre;
> 
>    procedure Lee_Nombres (Agenda : out Agenda_Type) is
>       Pos : Integer := 1;
>       Pos : Agenda_Index;
>    begin
>       Pos := Regresa_Libre(Agenda);
>       Flush;
>       Put_Line("La posicion es: ");
>       Put(Pos);
>       if Pos = 0 then
>          Put_Line("No hay espacio en la agenda");
>       else
>          Put_Line("Nombre: ");
> -- This Get_Line do not execute!!!
>          Get_Line(Agenda(Pos).nombre, Agenda(Pos).Len);
>       end if;
>    end Lee_Nombres;
> 
> begin
>    loop
>       Put_Line("** Agenda en Ada95 **");
>       Put_Line("");
>       Put_Line("1. Insertar registros a la agenda");
>       Put_Line("9. Salir");
>       Put_Line("");
>       Put("Opcion> ");
>       Get(Opc);

        case Opc is
          when Leer =>
          when Terminar =>
        end case;


>       exit when Opc = 9;
>       case Opc is
>          when 1 =>
>             Lee_Nombres(Agenda);
>          when others =>
>             Put_Line("Opcion Incorrecta!!!");
>       end case;
> 
>    end loop;
> 
>    Lee_Nombres( Agenda );
>    Put_Line("Los nombres leidos son:");
>    for I in 1 .. Max loop
>       Put_Line(Agenda(I).Nombre);
>       Put(Agenda(I).Len);
>    end loop;
> end Agenda;

I've added some hints about improvement. They are not complete, e.g. error input 
handling is missing (what happens if a letter rather than a number is input into 
your program when you read options?).

The problem why your Get_Line does not work is the following:

When you are prompted for the option "Opcion> ", you presumably enter a number and 
then hit the return key.
Get(Opc) just reads the number, leaving the end of line in the input stream.
The your Get_Line(Agenda(Pos).nombre, Agenda(Pos).Len) finds an empty string and 
returns 0 in Agenda(Pos).Len.

Try to enter "1   un_nombre" at the prompt and see what happens.

To solve the problem, add a Skip_Line after Get(Opc).




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

end of thread, other threads:[~2002-10-02 15:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-26  5:35 Problem with Get_Line Mauricio Tellez
2002-09-27 19:51 ` Frank J. Lhota
2002-10-02 15:22   ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
2002-09-26  5:34 Grein, Christoph

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