comp.lang.ada
 help / color / mirror / Atom feed
* File Problem. Please help..
@ 1996-07-31  0:00 Michael W. Hall
  1996-08-01  0:00 ` Michael W. Hall
  0 siblings, 1 reply; 4+ messages in thread
From: Michael W. Hall @ 1996-07-31  0:00 UTC (permalink / raw)



I am in ad ADA class at school and I am writing a program that reads ina 
string, a float and an integer from a file and then prints a graph.
I keep getting "exception not handled Data_error" when I run this program. I 
have included the data file and code below. It obvouisly is trying to read 
something different, when it is supposed to read an integer. I have put 
arrows in the line of code where it crashes. Also how do I have it read 
string variables of different lengths without know the lengths. The program 
isnt very large, so if you have time look at it and please help me if you 
can. Thanks

mhall59@us.net

****************
File : Data.In *
****************

Orville's Acres
114.8     43801
Hoffman's Hills
 77.2    36229
Jiffy Quick Farm
 89.4    24812
Jolly Good Plantation
183.2   104570
Organically Grown Inc.
 45.5    14683




***************
Program below *
***************

with Text_IO;

procedure Graph2 is

-- This program generates bar graphs for popcorn production.
-- Instantiate packages for numeric I/O

   package Integer_IO is new Text_IO.Integer_IO (Num => Integer);
   package Float_IO is new Text_IO.Float_IO (Num => Float);

-- Variables for main program
   Name : String(1..15);       -- Plant Name
   Result : Float;
   Popcorn_Data : Text_IO.File_Type;
------------------------------------------

procedure Print_Header is

   -- This procedure prints the title for the bar graph.
   -- The scale uses one mark per 250 pint jars.

   begin  -- Print_Header
      Text_IO.Put ("                  Pop Co-Op");
      Text_IO.New_Line;
      Text_IO.Put ("Farm Name                   Production");
      Text_IO.New_Line;
      Text_IO.Put ("                            Thousands of");
      Text_IO.New_Line;
      Text_IO.Put ("                            Pint Jars per Acre");
      Text_IO.New_Line;
      Text_IO.Put ("                               1   2   3   4   5   6");
      Text_IO.New_Line;
      Text_IO.Put ("                            ---|---|---|---|---|---|");
      Text_IO.New_Line;
   end Print_Header;

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

   procedure Get_Data (Data_File : in Text_IO.File_Type;
		       Pints_Per_Acre : out  Float;
		       Plant_Name : out String) is

   -- This procedure takes an input file as a parameter, reads
   -- the number of pints and number of acres that file.
   Total : Float;
   Acres : Float;
   Pints : Integer;

   begin   -- Get_Data
      Text_IO.Get (File => Data_File, Item => Plant_Name);
      Float_IO.Get (File => Data_File, Item => Acres);
*** crashes ***--->      Integer_IO.Get (File => Data_File, Item => Pints);
      Total := float(pints) / acres;
 
end Get_Data;



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

   procedure Print_Data (Plant_Name : in String;
			 Total : in Float;
			 Pints_Per_Acre : in Float) is
   -- This procedure prints the asterisk line, one for every 250 thousand.
   Total_Remaining : Float;
   begin  -- Print_Data
      Text_IO.Put (Item => Plant_Name);
      Total_Remaining := Pints_Per_Acre;
      Star_Loop:
         loop
	    exit Star_Loop when Total < 250.0;
	    Text_IO.Put("*"); -- Print "*" for each 250
	    Total_Remaining := Total_Remaining - 250.0;
	 end loop Star_Loop;
	 Text_IO.New_Line;
   end Print_Data;
------------------------------------------------

begin
   Text_IO.Open (File => Popcorn_Data,
                 Mode => Text_IO.IN_FILE,
                 Name => "data.in");
   Print_Header;
  -- Name := "                             ";
   Popcorn_Loop:
      loop
	 exit Popcorn_Loop when Text_IO.End_Of_File(Popcorn_Data);
	 Get_Data   (Data_File => Popcorn_Data,
		     Pints_Per_Acre => Result,
		     Plant_Name => Name);
	 Print_Data (Total => Result,
		     Pints_Per_Acre => Result,
		     Plant_Name => Name);
      end loop Popcorn_Loop;
	 Text_IO.New_Line;
end Graph2;





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

* Re: File Problem. Please help..
  1996-08-01  0:00 ` Michael W. Hall
@ 1996-08-01  0:00   ` John Herro
  1996-08-03  0:00     ` Michael W. Hall
  0 siblings, 1 reply; 4+ messages in thread
From: John Herro @ 1996-08-01  0:00 UTC (permalink / raw)



mhall59@mail.us.net (Michael W. Hall) writes:
> I have figured out that I got an error because
> my values were higher than 32767(or whatever
> is the largest int value) How can I use a 16 bit
> or 32 bit number? Is there a way to set this?
     First, it looks like you're using an Ada 83 compiler, so I'll answer
in terms of Ada 83.  The "cheap and dirty" way to fix the problem of
integer values higher than 32767 is to see if your compiler provides a
type Long_Integer, and use that instead of Integer.
     However, I don't recommend that, because Ada provides a much more
portable solution!  Looking at your data file, I see that your integers
range up to 104570.  Let's be conservative and say that your integers can
range up to a half million.  In Ada you can define a user-defined type:

type My_Int is range 0 .. 500_000;

Now, use My_Int instead of Integer (or Long_Integer).  You'll have to
instantiate Integer_IO for the type My_Int.
     With the declaration above, each Ada compiler will automatically
select the appropriate integer type.  A compiler that implements type
Integer with 32 bits will select Integer, while a compiler that implements
type Integer with 16 bits will select Long_Integer if it's available.  All
automatically, so your code is perfectly portable!
> Also how can I read the strings into a single
> variable while looping if they are different lengths?
     I like to use Get_Line for strings instead of Get.  You need to
declare not only the String variable (Name), but its length as an Integer
(e.g., Name_Len : Integer;).  Then do
Text_IO.Get_Line(File => Data_File, Item => Name, Last => Name_Len);
Name_Len will contain the number of characters in the line you read in,
and the first Name_Len characters of Name will be stored into (the rest of
Name will be unchanged).  If you don't want your Get_Data to store into a
global variable, add an extra out parameter for the length, and store into
that parameter and Plant_Name.
     I hope this helps.  For more about user-defined types and
portability, download the Ada Tutor program at the Web or FTP site below
my signature.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




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

* Re: File Problem. Please help..
  1996-07-31  0:00 File Problem. Please help Michael W. Hall
@ 1996-08-01  0:00 ` Michael W. Hall
  1996-08-01  0:00   ` John Herro
  0 siblings, 1 reply; 4+ messages in thread
From: Michael W. Hall @ 1996-08-01  0:00 UTC (permalink / raw)



I have figured out that I got an error because my values were higher 
than 32767(or what every is the largest int value) How can I use a 16 
bit or 32 bit number? Is there a way to set this?
Also how can I read the strings into a single variable while looping if 
they are different lengths? See above posting for data.

mhall59@us.net





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

* Re: File Problem. Please help..
  1996-08-01  0:00   ` John Herro
@ 1996-08-03  0:00     ` Michael W. Hall
  0 siblings, 0 replies; 4+ messages in thread
From: Michael W. Hall @ 1996-08-03  0:00 UTC (permalink / raw)



Thanks for the help. I got the answers I needed.





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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-31  0:00 File Problem. Please help Michael W. Hall
1996-08-01  0:00 ` Michael W. Hall
1996-08-01  0:00   ` John Herro
1996-08-03  0:00     ` Michael W. Hall

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