with Ada.Text_Io;
-- all statements in
with Ada.Integer_Text_Io; -- enables the reading and writing of
integers
procedure Inclassdemo is -- procedure body header
-- declarations go here
type Move is
(Scissors,
Paper,
Rock);
package Move_Io is new Ada.Text_Io.Enumeration_Io (Move);
-- package integer_io is new ada.text_io.integer_io (integer);
--
above statement was the old instantiation for integers
type Small_Int
is new Integer range 1 .. 10;
package Small_Int_Io is new Ada.Text_Io.Integer_Io
(Small_Int);
-- instantiation for small_int values
Computer_Move : Move;
User_Move : Move;
-- variables added to test type compatibility
i : integer := 17;
j : small_int := 5;
begin
ada.integer_text_io.put (i);
small_int_io.put(j);
-- statements that didn’t work are commented out with the captured reasons
-- j := i; -- don't expect this to work -- 17 is out of
j's range
-- inclassdemo.adb:28:12:
expected type "Small_Int" defined at line 14
-- i := j; --
might work because 5 is in i's range
-- inclassdemo.adb:30:22: expected type
"Standard.Integer"
--In
Ada.Text_Io.Put (
" Please
enter a move for the user, it may be rock, paper or
scissors ");
begin
Move_Io.Get
(User_Move);
Exception
when others =>
Ada.text_io.put_line (" you did not
enter an appropriate value ");
Ada.text_io.put (" Please enter: rock, paper or scissors ");
Move_io.get (User_Move); -- error here isn’t caught
-- tried to catch it with a
nested exception handler – doesn’t work
-- Exception
-- when others =>
-- Ada.text_io.put_line (" you did not enter an
appropriate value ");
-- Ada.text_io.put (" Please enter: rock, paper or scissors ");
-- Move_io.get (User_Move);
-- LEARNED THAT EXCEPTION HANDLERS CAN’T BE NESTED
end;
Ada.Text_Io.Put (
" Please enter a move for the computer, it may be rock, paper or scissors ");
begin
Move_io.get (Computer_move);
exception
when others =>
Ada.text_io.put_line (" you did not
enter an appropriate value ");
Ada.text_io.put (" Please enter: rock, paper or scissors ");
Move_io.get (Computer_Move); -- error
here bombs program
end;
Ada.Text_Io.Put ( " Here is what the user entered ");
Move_Io.Put
(User_Move);
Ada.Text_Io.New_Line (1);
Ada.Text_Io.Put
(" here is what the computer's move is ");
Move_Io.Put
(Computer_Move);
ada.text_io.new_line(1);
for I in move'range loop
--various
--associated with them
-- range is an
attribute associated with enumerated types
--
some other attributes associated with enumerated types
are:
-- 'first
'last 'pred 'succ
'val 'ord
move_Io.Put
(item =>I, width => 20);
end loop;
-- here are examples of using the attributes to print values
move_io.put (move'first);
move_io.put (move'last);
move_io.put (move'pred (move'last));
--
next line doesn’t work because rock doesn’t have a successor
--
move_io.put (move'succ (rock));
inclassdemo.adb:74:27: Succ of type'Last
move_io.put (move'succ (paper));
end Inclassdemo;