A little example of using generics, producing a menu, and pretry printing:
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with Ada.Characters.Handling;
--generic
-- type Menu_Choice is (<>);
procedure Get_Menu_Choice (Choice:out Menu_Choice) is
Input:Integer range 0..Menu_Choice'Pos(Menu_Choice'Last);
Done :Boolean;
function Pretty(S:String) return String is
-- This code is independent of 'image behaviuor.
-- just deals with _ and capitalizes first letter in "words"
Aux :String(1..S'Length):=(others=>' ');
Capital:Boolean:=True;
begin
for i in S'range loop
if S(i)='_' then
Aux(i):=' ';
Capital:=True;
elsif Capital then
Aux(i):=Ada.Characters.Handling.To_Upper(S(i));
Capital:=False;
else
Aux(i):=Ada.Characters.Handling.To_lower(S(i));
end if;
end loop;
return Aux;
end;
begin -- Get_Menu
for Choice in Menu_Choice loop
put_line(Integer'Image(Menu_Choice'Pos(Choice)) & ' ' &
Pretty(Menu_Choice'image(Choice)));
end loop;
loop
begin
get(Input);
Done:=True;
exception
when others =>
put_line("minimal error message");
Skip_Line;
Done:=False;
end;
exit when Done;
end loop;
Choice:=Menu_Choice'val(Input);
end;
And a simple driver:
with get_menu_choice;
with ada.text_Io;
use ada.text_io;
procedure try_menu_choice is
type menu is (First_Option,Second_Option_For_You);
procedure Get_My_Menu_Choice is new Get_Menu_Choice(Menu_Choice=>Menu);
package EIO is new Enumeration_Io(Menu);
use EIO;
C:menu;
begin
get_My_Menu_Choice(C);
put(C);
end;
|