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.
Here is a version of your program with change allowing it to compile without any compiler messages. You should test it to ensure it does what you want.
with Ada.Text_IO; use Ada.Text_IO;
procedure Momstabellen is
type Money is delta 0.01 digits 8 range 0.0 .. 999_999.99;
package money_io is new Decimal_IO (Money);
use money_io;
subtype Procent is Money range 0.0 .. 100.0;
Forsta_Pris : Money;
Sista_Pris : Money;
Steglangd : Money;
Momsprocent : Procent;
Moms : Money;
Antal_Rader : Money;
Nuvarande_Rad : Money;
begin
-- 1. Ta emot alla variabler för momstabellen:
Put ("Första pris: ");
Get (Forsta_Pris);
-- Definition of Money prohibits a value less than 0.0
-- 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;
loop
Put ("Sista pris: ");
Get (Sista_Pris);
exit when Sista_Pris >= Forsta_Pris;
Put_Line ("Felaktigt värde!");
end loop;
-- Again, the definition of Money prevents input of value < 0.0
Put ("Steg: ");
Get (Steglangd);
-- The definition of the subtype Procent ensures a value within the
-- required range.
Put ("Momsprocent: ");
Get (Momsprocent);
-- 2. Skapa en layout för momstabellen:
Put ("============ Momstabell ============");
New_Line;
Put ("Pris utan moms Moms Pris med moms");
New_Line;
Antal_Rader := (Sista_Pris - Forsta_Pris) / Steglangd;
for I in 0 .. Integer (Antal_Rader) loop
Nuvarande_Rad := Antal_Rader * Steglangd + Forsta_Pris;
Moms := Nuvarande_Rad / 100.0;
Moms := Moms * 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;