Reputation:
For this code, I need help with writing subprograms "Put" and "Make_Price". Subprogram "Put" should take a "Separator" parameter (a character that separates dollars from cents with ':' and ','). And subprogram "Make_Price" should take two Integers and then return dollars and cents (as it shows in the main program, P1, and P2).
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Procedure Price is
type Price_Type is
record
Dollar,Cents: Integer;
end record;
P1, P2 : Price_Type;
procedure Put(Item: out Price_Type;
Item_2: out Character) is
Separator:Character;
begin
Put_Line(Item.Dollar, Separator); --Error: too many arguments in call to
--"Put_Line"
Put(Item.Cents);
end Put;
function Make_price(Left, Right: in Price_Type) return Price_Type is
L,R: Integer;
begin
L:= Left.Dollar;
R:= Right.Cents;
return L; --Error: expected type "Price_Type"
end Make_price;
begin
P1 := Make_Price(19, 50);
P2 := Make_Price(10, 00);
Put_Line("First price: ");
Put(P1, Separator => ':'); --Prints out with ':'
New_Line;
Put_Line("Second price: ");
Put(P2, Separator => ','); -- Prints out with ','
New_Line;
end Price;
Upvotes: 1
Views: 274
Reputation: 25491
Your procedure Put
:
procedure Put(Item: out Price_Type;
Item_2: out Character) is
Separator:Character;
begin
Put_Line(Item.Dollar, Separator);
Put(Item.Cents);
end Put;
out
parameters? These are obviously in
mode (which is the default, so it’s common practice to omit it).Separator
isn’t initialized. From the way you use it below, the second parameter should be named Separator
.Ada.Text_IO
, indicates that the procedure shouldn’t output any new lines at all.Put_Line
which takes an Integer
and a Character
.Your procedure Make_Price
:
function Make_price(Left, Right: in Price_Type) return Price_Type is
L,R: Integer;
begin
L:= Left.Dollar;
R:= Right.Cents;
return L;
end Make_price;
Left
should be named Dollar
(well, I’d’ve named it Dollars
, but there you go). Right
should be named Cents
. Both should be of type Integer
(as shown by the way the function is called below).Price_Type
, so return L;
can’t be right. You need to make the record; (Dollar => Dollar, Cents => Cents)
compiles, but Price_Type'(Dollar => Dollar, Cents => Cents)
is better.Getting the output formatted better would be the subject of a different question. Or see Jim’s answer.
Upvotes: 1
Reputation: 5021
If you are interested in formatting money output look at the package Ada.Text_IO.Editing described in Appendix F.3.3 of the Ada Language Reference Manual. An example of using this package to provide differing radix marks is shown below.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Editing; use Ada.Text_IO.Editing;
procedure Main is
type Money is delta 0.01 digits 8;
package Money_IO is new Decimal_Output(Num => Money);
use Money_IO;
P1 : Picture := To_Picture("-$$$_$$$.$$");
price_1 : Money := 19.50;
price_2 : Money := 10.00;
begin
Put(File => Standard_Output, Item => Price_1, Pic => P1, Radix_Mark => ':');
New_Line;
Put(File => Standard_Output, Item => Price_2, Pic => P1, Radix_Mark => ',');
New_Line;
end Main;
The output of the above program is:
$19:50
$10,00
Upvotes: 4