comp.lang.ada
 help / color / mirror / Atom feed
* Data_Error - Where in this code?
@ 2001-08-06 19:47 Stewart
  2001-08-07 13:53 ` Mark Johnson
  2001-08-08  9:31 ` Stewart
  0 siblings, 2 replies; 7+ messages in thread
From: Stewart @ 2001-08-06 19:47 UTC (permalink / raw)


Hi,


Help'

The two procedures work seperately on Linux and jointly on Win98.
On linux I get the following data error using gnatmake.

a-tiinio.adb:71 instantiated at latest.adb:9

>   with Ada.Text_Io;
>   use Ada.Text_Io;
>--with Sorts;
>
>   procedure latest is 
>   
>   
>      type Edge_Cost is range 1 .. 999; 
>      package Cost_Io is new Integer_Io(Edge_Cost);
>      package Int_Io is new Integer_Io(Integer);
>      use   Cost_Io, Int_Io;  
>   
>      Max_Size, Int_Range  : Natural;  
>      Input     				: String (1 .. Max_Size);
>	Node_A    				: String (1 .. 6);  
>      Node_B    				: String (1 .. 6);  
>   	Edge 					: Edge_Cost;
> 
>--    this procedure reads the external network text file --
>--    for use by the other procedures.                    --
>      procedure Load_Locations is 
>         Location_File : File_Type; -- variable of type file. --  
>--         Space : Character;  Need for Win98
>  
>      begin
>      
>         Put_Line("Opening Network.txt for reading");new_line;
>         Open(Location_File, Mode => In_File, Name =>
>               "network.txt"); -- opening text file for reading. --
>      
>         while not End_Of_File(Location_File) loop
>--          stop at end of external text file.
>         
>            Get(Location_File,Node_A); --get the first name.
>--          Get(Location_File,Space);  --move pointer for next get in Win98.
>            Get(Location_File,Node_B); --get the second name. 
>            Get(Location_File,Edge);
>--          distance between node_a and node_b.
>
>         end loop;
>         Close(Location_File); --close the network.txt file after input.
>      end Load_Locations;
>   
>      procedure Menu is -- required input command structure -- 
>      
>         Add       : String (1 .. 3)        := "add";  
>         Remove    : String (1 .. 6)        := "remove";  
>         Link      : String (1 .. 4)        := "link";  
>         Cost      : String (1 .. 4)        := "cost";  
>         Quit      : String (1 .. 4)        := "quit";  
>
>      begin
>         loop
>            begin		-- Block for zero or negative cost in link --
>               New_Line; Put("Please enter command: ");
>               Get_Line(Input, Max_Size);
>               
>               if Input(1..3) = Add and  10 = Max_Size then
>                  Node_A := Input(5..10); New_Line;
>               
>               
>               elsif Input(1..6) = Remove and  13 = Max_Size then
>                  Node_A := Input(8..13); New_Line;
>               
>               
>               elsif Input(1..4) = Link and  19 < Max_Size and Max_Size < 23 then
>--                  Node_A := Input(6..11); 
>--                  Node_B := Input(13..18); 
>                  Int_Range := Max_Size - 19; New_Line;
>               
>                  case Int_Range is -- Mapping ascii numerals to integers --
>                     when 1 =>
>                     Edge  := Edge_Cost'Value(Input(19..Max_Size));
>--                   Convert a single numeral   --
>                  
>                     when 2 =>
>                     Edge  := Edge_Cost'Value(Input(20..21));
>--                   Convert two numerals        --
>                  
>                     when 3 =>
>                     Edge  := Edge_Cost'Value(Input(20..22));
>--                   Convert three numerals       --
>                  
>                     when others => null;
>                  end case;
>               
>               
>               elsif Input(1..4) = Cost and  11 = Max_Size then
>                  Node_A := Input(6..11);
>               
>               
>               elsif Input(1..4) = Quit then
>                  exit when Input(1..4) = Quit; new_line;
>               
>               else
>                  Put ("Sorry, do not understand command."); New_Line;
>                  Put ("Try again."); New_Line;
>               end if;
>               exception
>                  when CONSTRAINT_ERROR =>
>                     Put("Error in Cost Value. Enter 1 to 999 only.");
>            end;		-- End Block for zero or negative cost in link --
>         end loop;
>      end Menu;
>   
>   
>   begin
>   
>      Load_Locations;new_line;
>      Put_Line(" Network.txt loaded successfully!!!");new_line;
>      menu;
>      Put(Edge);
>   end latest;



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

* Re: Data_Error - Where in this code?
  2001-08-06 19:47 Data_Error - Where in this code? Stewart
@ 2001-08-07 13:53 ` Mark Johnson
  2001-08-08  9:31 ` Stewart
  1 sibling, 0 replies; 7+ messages in thread
From: Mark Johnson @ 2001-08-07 13:53 UTC (permalink / raw)


Stewart wrote:

> Hi,
>
> Help'
>
> The two procedures work seperately on Linux and jointly on Win98.
> On linux I get the following data error using gnatmake.
>
> a-tiinio.adb:71 instantiated at latest.adb:9
>

Hmm. Not on my system. I get the following messages from gnatmake....


$ gnatmake -g -k -f latest
gcc -c -g latest.adb
latest.adb:14:64: warning: "Max_Size" may be referenced before it has a value
gnatbind -x latest.ali
gnatlink -g latest.ali
/usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a(a-adaint.o): In
function `__gnat_tmp_name':
a-adaint.o(.text+0x504): the use of `tmpnam' is dangerous, better use `mkstemp'

And if you try to run the program, STORAGE_ERROR is raised at line 14 because
Max_Size is indeed referenced before it has a value.

This is with gnat 3.15w (and I assume 3.14a1) as well.

The error you refer to points to where DATA_ERROR is raised in the Ada run time. It
would indicate that your input file doesn't have an integer at the point you are
trying to read. You may wish to put in a little debug code to see what characters the
program is trying to read when "get" is being called. [or alternatively, resend the
program with a sample input file...]

  --Mark





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

* Re: Data_Error - Where in this code?
  2001-08-06 19:47 Data_Error - Where in this code? Stewart
  2001-08-07 13:53 ` Mark Johnson
@ 2001-08-08  9:31 ` Stewart
  2001-08-08 13:08   ` Mark Johnson
  1 sibling, 1 reply; 7+ messages in thread
From: Stewart @ 2001-08-08  9:31 UTC (permalink / raw)


On Mon, 06 Aug 2001 20:47:41 +0100, Stewart <s_aitken@lineone.net>
wrote:
Hi,

I still get the Max_Size is not declared, but it is not defined until
the variable for the input is obtained. If I put a value in Max_Size I
get the constraint_error for the value declared.

It now functions as required in both Win98/Dos and Linux.
Gnat 3.13.

I just have to glue it to the load procedure now.


Thanks

Stewart

>-- Menu commands  --
>-- 8 Aug  working properly   --
>
>   with Ada.Text_Io;
>   use Ada.Text_Io;
>   procedure Menu is
> 
>      type Edge_Cost is new Integer range 1 .. 999;
>      package Cost_InOut is new Integer_Io(Edge_Cost);
>      package Number_InOut is new Integer_Io(Integer);
>      use Cost_InOut, Number_InOut;
>  
>      Max_Size  : Positive;  
>      Int_Range : Natural; 
>      Edge      : Edge_Cost; 
>      Input     : String (1 .. Max_Size);  
>      Add       : String (1 .. 3)        := "add";  
>      Remove    : String (1 .. 6)        := "remove";  
>      Link      : String (1 .. 4)        := "link";  
>      Cost      : String (1 .. 4)        := "cost";  
>      Quit      : String (1 .. 4)        := "quit";  
>      Nodea     : String (1 .. 6); Nodeb     : String (1 .. 6);  
>       
>   
>   begin
>      loop
>         begin -- Block to contain the exception.   --
>            New_Line; Put("Please enter command: ");
>            Get_Line(Input, Max_Size); New_Line;
>            if Input(1..3) = Add and  10 = Max_Size then
>               Nodea := Input(5..10); New_Line;
>            elsif Input(1..6) = Remove and  13 = Max_Size then
>               Nodea := Input(8..13); New_Line;
>            elsif Input(1..4) = Link and 19 < Max_Size and Max_Size < 23 then
>               Nodea := Input(6..11); 
>               Nodeb := Input(13..18); 
>               Int_Range := Max_Size - 19; New_Line;
>            
>               case Int_Range is
>                  when 1 =>
>                     Edge  := Edge_Cost'Value(Input(19..20));
>               --                   Convert a single numeral   --           
>                  when 2 =>
>                     Edge  := Edge_Cost'Value(Input(20..21));
>               --                   Convert two numerals        --                 
>                  when 3 =>
>                     Edge  := Edge_Cost'Value(Input(20..22));
>               --                   Convert three numerals       --              
>                  when others => null; -- To capture case 0 and 4 --
>               end case;                 
>            elsif Input(1..4) = Cost and  11 = Max_Size then
>               Nodea := Input(6..11); 
>            elsif Input(1..4) = Quit then
>               exit when Input(1..4) = Quit;
>            else
>               Put ("Sorry, do not understand command."); New_Line;
>               Put ("Try again."); New_Line;
>            end if;
>            exception
>               when CONSTRAINT_ERROR =>
>                  Put("Error in Cost Value. Enter 1 to 999 only.");
>                  new_line;
>         end;      --  End Block to contain exception            --
>      end loop;
>   end Menu;




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

* Re: Data_Error - Where in this code?
  2001-08-08  9:31 ` Stewart
@ 2001-08-08 13:08   ` Mark Johnson
  2001-08-09  9:34     ` Stewart
  0 siblings, 1 reply; 7+ messages in thread
From: Mark Johnson @ 2001-08-08 13:08 UTC (permalink / raw)


Stewart wrote:

> On Mon, 06 Aug 2001 20:47:41 +0100, Stewart <s_aitken@lineone.net>
> wrote:
> Hi,
>
> I still get the Max_Size is not declared, but it is not defined until
> the variable for the input is obtained.

And as such - Max_Size cannot be used in an expression prior to its
initialization. The line...
  >      Input     : String (1 .. Max_Size);
is such an expression. It requires the compiler to allocate "Max_Size" bytes of
storage for the "Input" string. Let's say the Ada runtime gave you a default
value of zero (0) for Max_Size - the statement above be equivalent to...
 >      Input     : String (1 .. 0);
which I'm pretty sure is not what you wanted.... If you have something like gvd
(GNU visual debugger), I suggest stepping through the code to illustrate this
concept.

Why not use...
  Max_Size : Positive := 80; -- make it longer than any expected input line
instead. Text_IO.Get_Line will read up to 80 characters from the input line & set
Max_Size to the number of characters read. The rest of the code is pretty user
hostile, but "should work" as you expect.
  --Mark





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

* Re: Data_Error - Where in this code?
  2001-08-08 13:08   ` Mark Johnson
@ 2001-08-09  9:34     ` Stewart
  2001-08-09 13:27       ` Mark Johnson
  0 siblings, 1 reply; 7+ messages in thread
From: Stewart @ 2001-08-09  9:34 UTC (permalink / raw)


On Wed, 08 Aug 2001 08:08:24 -0500, Mark Johnson
<mark_h_johnson@raytheon.com> wrote:

>Stewart wrote:
>
>> On Mon, 06 Aug 2001 20:47:41 +0100, Stewart <s_aitken@lineone.net>
>> wrote:
>> Hi,
>>

>And as such - Max_Size cannot be used in an expression prior to its
>initialization. The line...
>  >      Input     : String (1 .. Max_Size);
>is such an expression. It requires the compiler to allocate "Max_Size" bytes of
>storage for the "Input" string. Let's say the Ada runtime gave you a default
>value of zero (0) for Max_Size - the statement above be equivalent to...
> >      Input     : String (1 .. 0);

The problem appears to have gone away when I glued fileload and menu
together. I moved the package definitions and the Max_Size : Positive;
to the main procedure definition area.
>
>Why not use...
>  Max_Size : Positive := 80; -- make it longer than any expected input line
>instead. Text_IO.Get_Line will read up to 80 characters from the input line & set
>Max_Size to the number of characters read. 

I tried this and the exception operated for the number I allocated to
Max_Size.

>The rest of the code is pretty user hostile, but "should work" as you expect.
>  --Mark

 Can I ask what you mean by user hostile?

I am using Skansholm's "Ada 95 from the beginning" as my learner's
bible.

any other advice gratefully received.

Stewart




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

* Re: Data_Error - Where in this code?
  2001-08-09  9:34     ` Stewart
@ 2001-08-09 13:27       ` Mark Johnson
  2001-08-09 17:55         ` Stewart
  0 siblings, 1 reply; 7+ messages in thread
From: Mark Johnson @ 2001-08-09 13:27 UTC (permalink / raw)


Stewart wrote:

> On Wed, 08 Aug 2001 08:08:24 -0500, Mark Johnson
> <mark_h_johnson@raytheon.com> wrote:
> [snip]
>
> >The rest of the code is pretty user hostile, but "should work" as you expect.
> >  --Mark
>
>  Can I ask what you mean by user hostile?

Well, let's demonstrate some "illegal" input...

"Add Abcdef" -- "add" is misspelled

"add Abcde" -- node names MUST be 6 characters long

"link abcdef uvwxyz " -- a 19 character line, Edge is not set (and no warning either)

If you read a line that is longer than your input buffer, no warnings, etc.

The program requires you to be overly precise - a pretty good definition of "user
hostile".

  --Mark




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

* Re: Data_Error - Where in this code?
  2001-08-09 13:27       ` Mark Johnson
@ 2001-08-09 17:55         ` Stewart
  0 siblings, 0 replies; 7+ messages in thread
From: Stewart @ 2001-08-09 17:55 UTC (permalink / raw)


On Thu, 09 Aug 2001 08:27:25 -0500, Mark Johnson
<mark_h_johnson@raytheon.com> wrote:

>Stewart wrote:
>
>> On Wed, 08 Aug 2001 08:08:24 -0500, Mark Johnson
>> <mark_h_johnson@raytheon.com> wrote:
>> [snip]
>>
>> >The rest of the code is pretty user hostile, but "should work" as you expect.
>> >  --Mark
>>
>>  Can I ask what you mean by user hostile?
>
>Well, let's demonstrate some "illegal" input...
>
>"Add Abcdef" -- "add" is misspelled
done
>"add Abcde" -- node names MUST be 6 characters long
done
>"link abcdef uvwxyz " -- a 19 character line, Edge is not set (and no warning either)
done
>If you read a line that is longer than your input buffer, no warnings, etc.
done
>The program requires you to be overly precise - a pretty good definition of "user
>hostile".
>
>  --Mark
The  'done' above all give the same message, that the command has not
been understood and to retry. It would be nice to have more specific
messages for each error but I am at my limit of understanding at the
moment. More practice required.

Code below

Thanks 

Stewart

>-- Main body combine load + menu  --
>-- Working in win98-dos & Linux. 8 Aug 2001    --
>
>   with Ada.Text_Io;
>   use Ada.Text_Io;
>--with Sorts;
>
>   procedure main is 
>      type Edge_Cost is new Integer range 1 .. 999;
>      package Cost_InOut is new Integer_Io(Edge_Cost);
>      package Number_InOut is new Integer_Io(Integer);
>      use Cost_InOut, Number_InOut;
>   
>      Max_Size  : Positive;
>   
>      procedure fileload is 
>      
>      -- this procedure reads the external text file for the pre-defined data.
>         procedure Load_Locations is 
>            Location_File : File_Type;  
>         --variable of type file.  
>            Node_A, Node_B : String (1 .. 6);  
>         --string of distance six.  
>            Edge_Weight : Edge_Cost;  
>         --distance between a b. 
>            Space : Character;  
>         
>         begin
>         
>            new_line;Put_Line(" Opening Network.txt for reading");new_line;
>            Open(Location_File, Mode => In_File, Name =>    "network.txt");
>         --opening text file for reading.
>         
>            while not End_Of_File(Location_File) loop
>            --stop at end of external text file.
>            
>               Get(Location_File,Node_A); --get the first name.
>               Get(Location_File,Space);  --move pointer.remove for linux
>               Get(Location_File,Node_B); --get the second name. 
>               Get(Location_File,Edge_Weight);
>            --distance between node_a and node_b.
>            
>            end loop;
>            Close(Location_File); --close the network.txt file after input.
>         end Load_Locations;
>      
>      begin
>         Load_Locations;
>         Put_Line(" Network.txt loaded successfully!!!"); New_Line;
>      end fileload;
>   
>      procedure Menu is
>      
>         Int_Range : Natural; 
>         Edge      : Edge_Cost; 
>         Input     : String (1 .. Max_Size);  
>         Add       : String (1 .. 3)        := "add";  
>         Remove    : String (1 .. 6)        := "remove";  
>         Link      : String (1 .. 4)        := "link";  
>         Cost      : String (1 .. 4)        := "cost";  
>         Quit      : String (1 .. 4)        := "quit";  
>         Nodea, Nodeb : String (1 .. 6);  
>         A, B : Character;
>         function checkchar (firstchar : Character) return Boolean is	
>         begin
>            return firstchar in 'a'..'z' or firstchar in 'A'..'Z';
>         end checkchar;		
>      
>      begin
>         loop
>         
>            begin -- Block to contain the exception.   --
>               Put("Please enter command: ");
>               Get_Line(Input, Max_Size);
>               if Input(1..3) = Add and  10 = Max_Size then
>                  Nodea := Input(5..10); New_Line;
>               elsif Input(1..6) = Remove and  13 = Max_Size then
>                  Nodea := Input(8..13); New_Line;
>               elsif Input(1..4) = Link and 19 < Max_Size and Max_Size < 23 then
>                  Nodea := Input(6..11); 
>                  Nodeb := Input(13..18); 
>                  Int_Range := Max_Size - 19; New_Line;
>               
>                  B := Nodeb(1); 
>                  if checkchar(B) then 
>                     null;
>                  else Put_Line(" Incorrect name for Node_B. ");
>                     Put_Line(" Name must start with a letter. "); New_Line;  
>                  end if;
>               
>                  case Int_Range is
>                     when 1 =>
>                        Edge  := Edge_Cost'Value(Input(19..20));
>                  --                   Convert a single numeral   --           
>                     when 2 =>
>                        Edge  := Edge_Cost'Value(Input(20..21));
>                  --                   Convert two numerals        --                 
>                     when 3 =>
>                        Edge  := Edge_Cost'Value(Input(20..22));
>                  --                   Convert three numerals       --              
>                     when others => null; -- To capture case 0 and 4 --
>                  end case;                 
>               elsif Input(1..4) = Cost and  11 = Max_Size then
>                  Nodea := Input(6..11); New_Line;
>               elsif Input(1..4) = Quit then
>                  exit when Input(1..4) = Quit;
>               else
>                  Put ("Sorry, do not understand command."); New_Line;
>                  Put_Line ("Try again."); New_Line;
>               end if;
>               exception
>                  when CONSTRAINT_ERROR =>
>                     Put("Error in Cost Value. Enter 1 to 999 only.");
>                     new_line;
>            end;      --  End Block to contain exception            --
>            A := Nodea(1); 
>            if checkchar(A) then
>               null;
>            else Put_Line(" Incorrect name for Node_A. ");
>               Put_Line(" Name must start with a letter. "); New_Line;            
>            end if;           
>         end loop;
>      end Menu;
>   
>   begin
>      fileload;new_line;
>      menu; new_line;
>   
>      Put(" It has been a pleasure serving you."); new_line;
>      Put(" Please return again soon.");
>   
>   end main;




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

end of thread, other threads:[~2001-08-09 17:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-06 19:47 Data_Error - Where in this code? Stewart
2001-08-07 13:53 ` Mark Johnson
2001-08-08  9:31 ` Stewart
2001-08-08 13:08   ` Mark Johnson
2001-08-09  9:34     ` Stewart
2001-08-09 13:27       ` Mark Johnson
2001-08-09 17:55         ` Stewart

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