Question

I am trying to write a sub procedure to read from a "grocery.txt" file. The format is as follow: 12 7.2 Orange

The first column is an integer, the second is a float. The third one is a String till the end of line. How should I declare the parameter and how can I pass the file variable as a parameter for the sub procedure?

 PROCEDURE Read_File(Ada.Text_IO.File_Type => in_Item,
  Item : out Grocery_Item) IS
 BEGIN
  Ada.Integer_Text_IO.Get(File => In_Item,
     Item => Grocery_Item.Quantity,
        Width => 3);
  Ada.Float_Text_IO.Get(File => In_Item, Item => Grocery_Item.Cost_Per_Item,
     Width => 3);
  Ada.Text_IO.Get_Line(Item => Grocery_Item.Item.Item_Name,
     Last => Grocery_Item.Item.Item_Name_Length);
 END Read_File;

I keep getting an error saying I am missing a "," on the PROCEDURE line.

Thank you very much.

Was it helpful?

Solution

=> is used only when calling procedures or functions. When you declare them, the syntax is parameter-name : [in/out/in out/access] type, just like you did with Item : out Grocery_Item:

procedure Read_File (In_Item : in Ada.Text_IO.File_Type;  -- the "in" keyword is optional
                     Item    : out Grocery_Item) is
...

(Just to avoid any possible confusion, => is also used in contexts that have nothing to do with procedures or functions (subprograms). The point is that when you're using subprograms, you may use => when you call them, but not when you declare them.)

Also, I'm not sure which one is the type and which one is the parameter name: Item or Grocery_Item? If Grocery_Item is the type, the parameter declaration is fine, but it doesn't make sense to use Grocery_Item.Quantity, Grocery_Item.Item.Item_Name_Length, etc., in the body, because that's not a place for a type name. Maybe you want to say Item.Quantity, etc. I can't tell. If Item is actually the type, and Grocery_Item will be the name of the parameter, then you need to switch them at the top of the procedure:

procedure Read_File (In_Item      : in Ada.Text_IO.File_Type;  -- the "in" keyword is optional
                     Grocery_Item : out Item) is
...

But to know for sure, we'd have to see what the declaration of either type Item or type Grocery_Item is.

Finally, one other thing: why do the first two Get calls use a file, but the last Get_Line doesn't? This will cause two reads from your file, and third read will be from the keyboard (probably). You probably want to add the File parameter to the last Get_Line too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top