comp.lang.ada
 help / color / mirror / Atom feed
From: "chris.danx" <spamoff.danx@ntlworld.com>
Subject: Re: [ot... well kindof] SDI
Date: Mon, 05 Aug 2002 19:22:07 +0100
Date: 2002-08-05T19:22:07+01:00	[thread overview]
Message-ID: <Ynz39.11$5d3.56@news13-win.server.ntlworld.com> (raw)
In-Reply-To: aik16p$2de6$1@msunews.cl.msu.edu

Chad R. Meiners wrote:
> "chris.danx" <spamoff.danx@ntlworld.com> wrote in message

>>   - "the following procedures use result codes:
>>      ...
>>
>>      Modify the procedures to use exceptions instead, ensuring that the
>>      effect is the same."
> 
> 
> I think this question would be suitable here since you can show both
> versions of the code and ask specific questions about the codes'
> equivalance.

this is the original code.


subtype Postcode is String (1..7);

type PersonRecord is
    record
       name    : ...;
       address : ...
       pc      : Postcode;
    end record;

-- Read a postcode from standard input into code.
-- Set ok to true if and only if that post code is
-- well-formed.
--
procedure get_postcode (pc : out Postcode;
                         ok : out Boolean) is
begin
    get (pc);
    ok := is_letter(pc(1)) and ...;
end get_postcode;


-- Read complete personal details from standard input into person,
-- interactively promting the user for each detail.
--
procedure get_personal_details (person : out PersonRecord) is
    pc_ok : Boolean;
begin
    ... -- read person.name and person.address.
    loop
       put ("enter postcode");
       get_postcode (person.pc, pc_ok);
       exit when pc_ok;
       put_line ("postcode is ill-formed!");
     end loop;
end get_personal_details


This is my revision to make it work with exceptions instead.

subtype Postcode is String (1..7);

type PersonRecord is
    record
       name    : ...;
       address : ...
       pc      : Postcode;
    end record;

postcode_error : exception;

-- return true if pc is well-formed, false otherwise.
--
-- I prefer this since postcode formats may change in future,
-- this makes a change easier!
--
function is_wellformed (pc : in postcode) return boolean  is ...;


-- Read a postcode from standard input into code.
--
-- raises a Postcode_Error exception if the postcode is ill-formed.
--
procedure get_postcode (pc : out Postcode) is
begin
    get(pc);
    if not is_wellformed (pc) then
       raise Postcode_Error;
    end if;
end get_postcode;

-- Read complete personal details from standard input into person,
-- interactively promting the user for each detail.
--
procedure get_personal_detials (person : out PersonRecord) is
begin
    ... -- read person.name and person.address.
    loop
       begin
          put ("enter postcode");
	
          get_postcode (person.pc, pc_ok);
          exit;  -- ???
       exception
          when Postcode_Error => Put_Line ("Postcode is ill-formed!");
       end;
     end loop;
end get_personal_details


My concern is at the line marked "-- ???" the code will just exit the 
begin-end block not the loop, is this correct?  If so a simple revision 
that introduced a flag set after get_postcode would do.

The effect of the code has to be the same, that is it must loop until a 
valid postcode is entered.



Chris

-- 
to reply change 'spamoff' to 'chris'




  reply	other threads:[~2002-08-05 18:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-04  0:24 [ot... well kindof] SDI chris.danx
2002-08-04 14:16 ` Robert Dewar
2002-08-04 18:19   ` tmoran
2002-08-04 19:08     ` chris.danx
2002-08-04 20:00       ` Chad R. Meiners
2002-08-05 18:22         ` chris.danx [this message]
2002-08-05 18:24           ` chris.danx
2002-08-05 19:46           ` Chad R. Meiners
2002-08-05 22:25             ` chris.danx
2002-08-05 23:17               ` Chad R. Meiners
2002-08-05 23:46                 ` chris.danx
2002-08-05 15:58       ` Stephen Leake
2002-08-05 16:10         ` chris.danx
2002-08-05  1:26     ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox