Question

Please have a look at the following code

Formula.ads

package Formula is
   procedure CalculateFormula;
end Formula;

Formula.adb

with Ada.Text_IO; use Ada.Text_IO;
with Formula; use Formula;

package body Formula is
   procedure CalculateFormula is

      package fom_io is new Ada.Float_Text_IO(Float);
      use fom_io;

      u : Float;
      t : Float;
      a : Float;
      answer : Float;

   begin
      put_line("Enter 'U'");
      get(u);

      put_line("Enter 'T'");
      get(t);

      put_line("Enter 'A'");
      get(a);

      answer = (u*t)+ 0.5(a(t*t));

      put("Answer is: ");
      put(answer,Fore => 1,Aft => 1,Exp => 1);
   end CalclualeFormula;
end Formula;

When I run this code, I get the following error

gnatmake -d -PC:\Users\yohan\Documents\Ada\Formula\formula.gpr
gcc -c -I- -gnatA C:\Users\yohan\Documents\Ada\Formula\formula.ads
cannot generate code for file formula.ads (package spec)
gnatmake: "C:\Users\yohan\Documents\Ada\Formula\formula.ads" compilation error

[2013-04-06 03:18:22] process exited with status 4 (elapsed time: 00.22s)

I am very new to Ada. Started coding few hours back. Please help me to get rid of the above issue. Thank you.

EDIT

formula.gpr

project Formula is

   for Main use ("formula.ads");

end Formula;
Was it helpful?

Solution

The GPR file shows that you are trying to use a package specification as a main program unit - that doesn't work.

Two ways to fix this: The simplest one is to make "CalculateFormula" a standalone main program in a file "calculateformula.adb" and set this in your project file:

for Main use ("calculateformula.adb");

But if you want to see how packages work, there is a "better" way (in that it gets you to understand what packages are for and how they are used...)

Packages are reusable components : the package spec is all you need to see in order to use them ... but you actually use them in another program. So in this case you would create a main program

with Formula; use Formula;

    procedure MyMain is
    begin
       CalculateFormula;
    end MyMain;

and in the .gpr file,

for Main use ("mymain.adb");

And then the compiler will automatically compile the correct packages and find the other issues that Shark has pointed out.

You really don't need a separate Main here, as the "hello world" example showed. However such trivial "main subprograms" (in the correct Ada lingo) are not so unusual, especially for purposes like unit testing packages to be used in more complex apps later.

OTHER TIPS

This is puzzling because there are some big errors that the compiler should be flagging, like:

package fom_io is new Ada.Float_Text_IO(Float);

which isn't withed, and

answer = (u*t)+ 0.5(a(t*t));

because:

  1. = is not the assignment operator; you need :=.
  2. 0.5(XXXX) isn't valid multiplication.

Also, there's exponentiation in Ada, so t**2 can replace t*t.


formula.adb

with
Ada.Float_Text_IO,
Ada.Text_IO;

use
Ada.Text_IO;

package body Formula is
   procedure CalculateFormula is

      use Ada.Float_Text_IO;

      u, t, a, answer : Float;

    Procedure Prompt( Item : out Float; Name : String ) is
    begin
        put_line("Enter '"& Name &"'");
        get(Item);
    end prompt;
   begin

    prompt( u, "U" );
    prompt( t, "T" );
    prompt( a, "A" );

      answer:= (u*t) + 0.5*( a*t**2 );

      put("Answer is: ");
      put(answer,Fore => 1,Aft => 1,Exp => 1);
      New_Line;
   end CalculateFormula;
end Formula;

This corrects the syntax errors you had. The other answer seems right in that this ["cannot generate code for" error] seems to be a problem with the gpr-file rather than the source itself.

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