❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

object "Antal_rader" cannot be used before end of its declaration

OBS! My code is written with Swedish terms.

I have to code a VAT table but i have run in to some problems concerning Floats and the way that it rounds values. At first, I was using a while loop but that didn't work since it (after a while) got too affected by the float-issue.

I created a For-loop instead which in theory should work better. However. I'm having an issue with one of my variables.

The variable "Antal_Rader" is being declared before the For-loop but i still get the error code: "object "Antal_rader" cannot be used before end of its declaration"

Code:

with Ada.Text_IO;              use Ada.Text_IO;
with Ada.Float_Text_IO;        use Ada.Float_Text_IO;

procedure Momstabellen is
   Forsta_Pris : Float;
   Sista_Pris : Float;
   Steglangd : Float;
   Momsprocent : Float;
   Moms : Float;
   Antal_Rader : Float;
   Nuvarande_Rad : Float;
   
   
begin
   -- 1. Ta emot alla variabler för momstabellen:
   Put("Första pris: ");
   Get(Forsta_Pris);
   while Forsta_Pris < 0.0 loop
      Put("Felaktigt värde!");
      New_Line;
      Put("Första pris: ");
      Get(Forsta_Pris);
    end loop;
   
   Put("Sista pris: ");
   Get(Sista_Pris);
   while Sista_Pris < Forsta_Pris loop
      Put("Felaktigt värde!");
      New_Line;
      Put("Sista pris: ");
      Get(Sista_Pris);
   end loop;
   
   Put("Steg: ");
   Get(Steglangd);
   while Steglangd < 0.0 loop
      Put("Felaktigt värde!");
      New_Line;
      Put("Steg: ");
      Get(Steglangd);
   end loop;
   
   Put("Momsprocent: ");
   Get(Momsprocent);
   while Momsprocent < 0.0 or Momsprocent > 100.0 loop
      Put("Felaktigt värde!");
      New_Line;
      Put("Momsprocent: ");
      Get(Momsprocent);
   end loop;
   New_Line;

-- 2. Skapa en layout för momstabellen:
   Put("============ Momstabell ============");
   New_Line;
   Put("Pris utan moms  Moms   Pris med moms");
   New_Line;

Issue lies here:

   

   Antal_Rader := (Sista_Pris - Forsta_Pris)/Steglangd;
   
   for I in 0 .. Antal_Rader loop
      
      Nuvarande_Rad := Antal_Rader * Steglangd + Forsta_Pris;   
      Moms := Nuvarande_Rad/100.0 * Momsprocent;
      
      Put(Nuvarande_Rad, Fore=> 6, Aft => 2, Exp => 0);
      Put(Moms, Fore => 8, Aft => 2, Exp => 0);
      Put(Nuvarande_Rad + Moms, Fore => 9, Aft => 2, Exp => 0);
     
   end loop;
    
     
end Momstabellen;

I have tried changing the first line of the For-loop and also tried changing the position of "Antal_Rader" but nothing has worked.

I am quite new to this so i can't think of many more things to do.

Gnatcheck custom rules

I'm trying to obtain a list of global variables declared in a large Ada project. gnatcheck has a rule for that: Global_Variables, but that rule is not exhaustive:

"Flag any variable declaration that appears immediately within the specification of a library package or library generic package. Variable declarations in nested packages and inside package instantiations are not flagged"

I'd like to be able to obtain the list of variables declared in package instantiations as well. I would try to implement my own gnatcheck custom rule, but cannot seem to find where the rules are implemented. This doc says it's supposed to be in share/lkql, but there is no such directory in my GNAT Studio installations (I've looked at GNAT Pro 19.2, GNAT Pro 22.1 and Gnat Community 2021). Windows cannot find any .lkql file.

Is it possible to make such a rule? If so, how/where? Are there alternatives to gnatcheck to obtain the list of global variables?

❌
❌