comp.lang.ada
 help / color / mirror / Atom feed
* Help with ADT Stack
@ 1997-05-29  0:00 Jon Elbery
  1997-05-29  0:00 ` Michael F Brenner
  1997-05-29  0:00 ` Help With Ada_Io and Exceptions David H. Haley
  0 siblings, 2 replies; 4+ messages in thread
From: Jon Elbery @ 1997-05-29  0:00 UTC (permalink / raw)



I have a problem with an assignment I am writing for university. You are
my last resort!!!

The program below works until I call line 259 ..I get a stack empty
exception.

Don't take any notice of the calls with cat, elephant etc..they will be
removed in the final version!!!!

I have tried everything I know (but am relatively new to Ada). Can
someone please point me in the right direction??

Any help appreciated. I am using Gnat307 running in Dos.

Penny

GNAT 3.07 (961023) Copyright 1991-1996 Free Software Foundation, Inc.

     1.
     2.
     3.
     4. --File Statlib.ADB
     5.
     6. with Ada.Text_IO, ADTBStk;
     7. use Ada.Text_IO;
     8. With Ada.Integer_Text_IO;
     9. use Ada.Integer_Text_IO;
    10. with GetInt;
    11.
    12.
    13. procedure Statlib is
    14.
    15.
    16. --Sub Types
    17. subtype NumTitlesType is Positive;
    18. subtype NumCopiesType is Positive;
    19. subtype SimTimeType is Natural;
    20. subtype IDCodeType is String (1..3);
    21. subtype LoanTimeType is Natural;
    22. subtype UsageTimeType is Natural;
    23. subtype CopyType is String (1..2);
    24. subtype TitleType is Character;
    25.
    26. subtype CopyRange is Integer range 1..5;
    27.
    28.
    29. type ItemType is
    30.    record
    31.       IDCode      : IDCodeType;
    32.       UsageTime   : UsageTimeType := 0;
    33.       LoanOutTime : LoanTimeType  := 0;
    34.    end record;
    35.
    36. package LibType is new ADTBSTK (ItemType);
    37. use LibType;
    38.
    39. --Types
    40. type MainChoiceType is (C, Q);
    41. type SimChoiceType is (L, R, A, T, M);
    42. type DispenserType is array (TitleType range <> ) of
LibType.Stack(5);
    43. type OnLoanType is array (TitleType, CopyRange) of ItemType;
    44.
    45.
    46. --Instantiations
    47.
    48. package MainChoice_IO is new Ada.Text_IO.Enumeration_IO
(MainChoiceType);
    49. use MainChoice_IO;
    50. package SimChoice_IO is new Ada.Text_IO.Enumeration_IO
(SimChoiceType);
    51. use SimChoice_IO;
    52.
    53.
    54. --Variables
    55. Choice         : MainChoiceType;
    56.
------------------------------------------------------------------------------

    57.
    58. procedure ProgramIntro is
    59.
    60. -- introduces the program
    61.
    62.     begin
    63.        Set_Col (30);
    64.        Put_Line ("*** Library Simulation ***");
    65.        New_Line (2);
    66.        Put_Line ("This program simulates the operation of a "
    67.                  & "library reserve system.");
    68.        New_Line (2);
    69.        Put_Line ("Please choose an option from the menu.");
    70.
    71.     end ProgramIntro;
    72.
------------------------------------------------------------------------------

    73.
    74. procedure GetMainChoice (Choice : out MainChoiceType) is
    75.
    76. -- displays the menu and gets the user's choice of
    77. -- commencing a simulation or quitting
    78.
    79.     begin
    80.        New_Line(2);
    81.        Set_Col (25);
    82.        Put ("**Main Menu**");
    83.        New_Line(2);
    84.        Set_Col (20);
    85.        Put ("C  Commence a simulation");
    86.        New_Line;
    87.        Set_Col (20);
    88.        Put ("Q  Quit the program");
    89.        New_Line;
    90.
    91.          InputLoop:
    92.          loop
    93.            declare
    94.            begin
    95.              New_Line;
    96.              Set_Col (20);
    97.              Put ("Your Choice:  ");
    98.              Get (Choice);
    99.              Skip_Line;
   100.              exit;
   101.
   102.            exception
   103.                when Data_Error =>
   104.                Skip_Line;
   105.                Set_Col (20);
   106.                Put_Line ("Invalid Choice, please try again.");
   107.            end;
   108.         end loop InputLoop;
   109.
   110.     end GetMainChoice;
   111.
------------------------------------------------------------------------------

   112.
   113. procedure GetNumTitles (NumTitles : out NumTitlesType) is
   114.
   115. -- prompts user for the number of titles
   116.
   117.     begin
   118.        GetInt(NumTitles, 1,3, " the number of titles");
   119.     end GetNumTitles;
   120.
------------------------------------------------------------------------------

   121.
   122. procedure CreateLoanOutArray (OnLoan : out OnLoanType) is
   123.
   124. -- creates the array for copies on loan
   125.
   126. --      CopyNo : CopyRange;
   127.
   128.     begin
   129.
   130.        for Title in TitleRange'Range loop
   131.           for CopyNo in CopyRange'Range loop
   132.                OnLoan(Title,CopyNo) :=("zzz", 0, 0);
   133.           end loop;
   134.        end loop;
   135.     end CreateLoanOutArray;
   136.
------------------------------------------------------------------------------

   137. procedure DoSimChoice (SimChoice : SimChoiceType);
   138.
   139. procedure Initialise   (Dispenser : in out DispenserType;
   140.                         OnLoan    : out OnLoanType;
   141.                         SimTime   : out SimTimeType) is
   142.
   143. -- Initialises all values for the simulation
   144.
   145.     NumTitles : NumTitlesType;
   146.     NumCopies : NumCopiesType;
   147.     Title     : TitleType;
   148.     Copy      : CopyType;
   149.     Item      : ItemType;
   150.     Cat,Elephant,Dog       : ItemType;
   151.     SimChoice : SimChoiceTYpe;
            |
        >>> warning: "SimChoice" is never assigned a value

   152.
   153.     begin
   154.        GetNumTitles (NumTitles);
   155.
   156.          for Count in 1..NumTitles loop
   157.            Title := Character'Val (Count + 64);
   158.            GetInt(NumCopies, 1, 5, "the number of copies for
Title " &
   159.                   Title);
   160.
   161.          declare
   162.             Dispenser : DispenserType('A'..'C');
   163.          begin
   164.             for Count in reverse 1..NumCopies loop
   165.               Copy :="0" & Character'Val(Count + 48);
   166.               Item.IDCode := Title & Copy;
   167.               Skip_Line;
   168.               Put(Item.IDCode);
   169.               Put ("Hell");
   170.               Skip_Line;
   171.               Item := ItemType(Item);
   172.               Push (Dispenser(Title),Item);
   173.               Cat := (Top(Dispenser(Title)));
   174.               New_Line;
   175.               Put(Cat.IDCode);
   176.               New_Line;
   177.               Put("Damn");
   178.               New_Line;
   179.               Put(Cat.UsageTime);
   180.
   181.          end loop;
   182.               Put (NumberofObjects(Dispenser(Title)));
   183.               Put ("lucy");
   184.               Skip_Line;
   185.               DoSimChoice (SimChoice);
   186.         end;
   187.         end loop;
   188.        Dog := (Top(Dispenser('A')));
   189.        Put (Dog.IDCode);
   190.        Put("Penny");
   191.        SimTime := 0;
   192.        Put (SimTime);
   193.        CreateLoanOutArray (OnLoan);
   194.        Elephant := OnLoan('B',2);
   195.        Skip_Line;
   196.        Put(Elephant.IDCode);
   197.
   198.     end Initialise;
   199.
------------------------------------------------------------------------------

   200.
   201. procedure GetSimChoice (SimChoice : out SimChoiceType) is
   202.
   203. -- displays the options for a simulation and get's the user's
choice
   204.
   205.     begin
   206.        New_Line(2);
   207.        Set_Col (25);
   208.        Put ("*** Options ***");
   209.        New_Line(2);
   210.        Set_Col(20);
   211.        Put ("L Loan request");
   212.        New_Line;
   213.        Set_Col(20);
   214.        Put ("R Return an item");
   215.        New_Line;
   216.        Set_Col(20);
   217.        Put ("A Advance simulation time");
   218.        New_Line;
   219.        Set_Col(20);
   220.        Put ("T Terminate the simulation");
   221.        New_Line;
   222.        Set_Col(20);
   223.        Put ("M Monitor the simulation");
   224.        New_Line;
   225.
   226.        InputLoop:
   227.        loop
   228.          declare
   229.          begin
   230.              New_Line;
   231.              Set_Col (20);
   232.              Put ("Your Choice:  ");
   233.              Get (SimChoice);
   234.              Skip_Line;
   235.              exit;
   236.
   237.            exception
   238.                when Data_Error =>
   239.
   240.                Skip_Line;
   241.                Set_Col (20);
   242.                Put_Line ("Invalid Choice, please try again.");
   243.            end;
   244.         end loop InputLoop;
   245.
   246.     end GetSimChoice;
   247.
------------------------------------------------------------------------------

   248.
   249. procedure GetLoanRequest (Title :out TitleType) is
   250.
   251. -- gets the user's choice of Title to borrow
   252.     Cat : ItemType;
   253.     Dispenser :DispenserType ('A'..'C');
   254.
   255.     begin
   256.        New_Line(2);
   257.        Put ("Which Title would you like to borrow? : ");
   258.        Get (Title);
   259.        Cat := (Top(Dispenser(Title)));
   260.        Put (Cat.IDCode);
   261.        Put("Tim");
   262.     end GetLoanRequest;
   263.
------------------------------------------------------------------------------

   264.
   265. procedure AdvanceSimTime (SimTime : in out SimTimeType) is
   266.
   267. -- advances simulation time by one unit
   268.
   269.     begin
   270.        SimTime := SimTime + 1;
   271.     end AdvanceSimTime;
   272.
------------------------------------------------------------------------------

   273.
   274. procedure DoLoan (SimTime   : in out SimTimeType;
   275.                   Title     : out TitleType;
   276.                   Dispenser : in out DispenserType;
   277.                   OnLoan    : in out OnLoanType) is
   278.
   279. -- takes the action to process a loan
   280.
   281.     Item, Cat      : ItemType;
   282.     CopyNo    : CopyRange;
            |
        >>> warning: "CopyNo" is never assigned a value

   283.
   284.     begin
   285. --       GetLoanRequest (Title);
   286.       -- ProcessItem (Item, SimTime);
   287.        New_Line(2);
   288.        Put ("Which Title would you like to borrow? : ");
   289.        Get (Title);
   290.        Cat := (Top(Dispenser(Title)));
   291.        Put (Cat.IDCode);
   292.        Put("Tim");
   293.        AdvanceSimTime (SimTime);
   294.     end DoLoan;
   295.
------------------------------------------------------------------------------

   296.
   297. procedure DoSimChoice (SimChoice : SimChoiceType) is
   298.
   299. -- takes the necessary action on user's choice of menu item
   300.
   301.     Dispenser : DispenserType;--('A'..'C');
   302.     OnLoan    : OnLoanType;
   303.     SimTime   : SimTimeType;
   304.     Title     : TitleType;
   305.
   306.     begin
   307.        case SimChoice is
   308.          when L =>
   309.            Put ("Do Loan");
   310.            DoLoan (SimTime, Title, Dispenser, OnLoan);
   311.          when R =>
   312.            Put ("Return");
   313.          when A =>
   314.            Put ("Advance");
   315.          when T =>
   316.            Put ("Stop");
   317.          when M =>
   318.            Put ("In your Dreams!");
   319.        end case;
   320.     end DoSimChoice;
   321.
------------------------------------------------------------------------------

   322.
   323. procedure DoSimulation is
   324.
   325. -- commences the simulation
   326.
   327.     Dispenser : DispenserType ('A'..'C');
   328.     OnLoan    : OnLoanType;
   329.     SimTime   : SimTimeType;
   330.     SimChoice : SimChoiceType;
   331.     Cat       : ItemType;
   332.
   333.     begin
   334.        Put ("Sim");
   335.        Initialise(Dispenser, OnLoan, SimTime);
   336.        Cat := Top(Dispenser('A'));
   337.        Put (Cat.IDCode);
   338.        GetSimChoice (SimChoice);
   339.        Put (Cat.IdCode);
   340.       -- DoSimChoice (SimChoice);
   341.     end DoSimulation;
   342.
------------------------------------------------------------------------------

   343.
   344. procedure Farewell is
   345.
   346. -- leaves the program
   347.
   348.     begin
   349.        Put ("Thank you for using this program");
   350.     end Farewell;
   351.
------------------------------------------------------------------------------

   352.
   353.     begin                                             -- Statlib

   354.        ProgramIntro;
   355.        loop
   356.
   357.           GetMainChoice (Choice);
   358.           exit when Choice = Q;
   359.           DoSimulation;
   360.        end loop;
   361.        Farewell;
   362.     end Statlib;

 362 lines:





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help with ADT Stack
  1997-05-29  0:00 Help with ADT Stack Jon Elbery
@ 1997-05-29  0:00 ` Michael F Brenner
  1997-05-29  0:00 ` Help With Ada_Io and Exceptions David H. Haley
  1 sibling, 0 replies; 4+ messages in thread
From: Michael F Brenner @ 1997-05-29  0:00 UTC (permalink / raw)



Here are a couple of questions:

The file statlib imports a package called ADTBStk, which we can guess
is intended to be an Abstract Data Type B-Tree implemented Stack, and
did you write this and is it fully tested and what is its specification
(visible part)?

Presumably the Push on line 172 is what is putting things onto the 
stack whose Top you are requesting, so what is the value of NumCopies
the program sees as the loop control variable on line 164?

By spending a lot of time reading and analyzing the code in this post,
it might be possible to find the source of this problem without seeing
the source code for the bdy of ADTBStk, because the bug might be in 
Statlib instead of ADTBStk, but that might not be the most efficient
way to do this.

Is Top programmed to fail when there are no elements on the stack?

Why is a stack being used to track the loans?






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Help With Ada_Io and Exceptions
  1997-05-29  0:00 Help with ADT Stack Jon Elbery
  1997-05-29  0:00 ` Michael F Brenner
@ 1997-05-29  0:00 ` David H. Haley
  1997-05-30  0:00   ` John G. Volan
  1 sibling, 1 reply; 4+ messages in thread
From: David H. Haley @ 1997-05-29  0:00 UTC (permalink / raw)



-- I'm having a bit of a problem with Ada_Io.get after a Data_Error is
raised,
-- and am wondering if anyone can shed some light on this.

-- Many thanks in advance...... 

WITH Ada_Io; USE Ada_Io; 
WITH Text_Io; USE Text_Io; 
--
-- adapted from ADA as a second language
-- by Norman H. Cohen Page 485
--
-- The purpose of this procedure is to check to see whether or not
-- the user has input valid data - i.e. an integer is expected; however,
-- if the user enters a character string e.g. "t" or "test" in lieu of
-- the expected integer, then the procedure should inform the user
-- of his/her error and prompt for correct data input
--
PROCEDURE T IS 
   My_Number          : Integer;
   Well_Formed_Number : Boolean; 
BEGIN
   Ada_Io.Put("Please enter a number between 1 and 10  "); 
   LOOP
      BEGIN
	 Ada_Io.Put("=> ");
	 Ada_Io.Get(My_Number); 
	 --
	 --              ***** Problem Area ****
	 --
	 -- if a keyboard character(s) is entered, i.e. "t" or "test"
	 -- the Exception Handler catches the Data_Error, then
	 -- the program will loop back up to the above two statements;
	 -- however, while the Put statement is correctly executed
	 -- the Get statment does not permitted any data entry and
	 -- the program endlessly loops. (An examination of the
	 -- assembly listing shows that the .asm file produced loops
	 -- back to the correct position; however, I have not
	 -- used an assembler to see if the expected action
	 -- actually occurs).
	 --
	 -- Thus, what is displayed after a character i.e. "t" is
	 -- entered is:
	 --             An error occurred
	 --             That is not a number
	 --             Please re-enter => An error occurred
	 --             That is not a number
	 --             Please re-enter => An error occurred
	 --             That is not a number
	 --             ...
	 --
	 -- the same situation occurs with Ada_Io and the Meridian
	 -- pre-defined library package Iio which consists of an
	 -- instantiation of Text_Io.Integer_Io for the standard
	 -- type integer
	 --
	 -- On page 635 of Cohen's book he states "As soon as
	 -- an unexpected character or terminator is encountered,
	 -- Get raises Data_Error, and no further characters are
	 -- read. The offending character or terminator remains
	 -- unread and can be processed by a subsequent input
	 -- operation if Data_Error is handled
	 --
	 -- Note that if you enter a number such as "888888888"
	 -- then the program loops as expected (in that it allows
	 -- a further entry of data in the Get statement)
	 -- after outputting the following error messages:
	 --
	 --            An error occurred
	 --            That is not a number
	 --
	 Well_Formed_Number := True; 
      EXCEPTION
	 WHEN Data_Error => 
	    Well_Formed_Number := False; 
	    Ada_Io.Put_Line("An error occurred"); 
	 WHEN OTHERS => 
	    Ada_Io.Put_Line("Program Abnormally Terminated"); 
      END; 
      EXIT WHEN Well_Formed_Number AND THEN My_Number IN 1 .. 10; 
      IF Well_Formed_Number THEN 
	 Ada_Io.Put_Line("That is not between 1 and 10."); 
      ELSE 
	 Ada_Io.Put_Line("That is not a number."); 
      END IF; 
      Ada_Io.Put("Please re-enter"); 
      DELAY (1.0);  -- for illustration purposes only
   END LOOP; 
END T;





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help With Ada_Io and Exceptions
  1997-05-29  0:00 ` Help With Ada_Io and Exceptions David H. Haley
@ 1997-05-30  0:00   ` John G. Volan
  0 siblings, 0 replies; 4+ messages in thread
From: John G. Volan @ 1997-05-30  0:00 UTC (permalink / raw)



David H. Haley wrote:
> 
> -- I'm having a bit of a problem with Ada_Io.get after a Data_Error is
> raised,
> -- and am wondering if anyone can shed some light on this.

This is a FAQ. The problem is, when an Integer_IO.Get fails with a
Data_Error, the offending characters aren't eaten up. They remain in
file input stream, ready to offend again.  Clearly, your exception
handler needs to do something more than simply re-trying the Get: It
must clean out those offending characters.

Try doing a Skip_Line.

------------------------------------------------------------------------
Internet.Usenet.Put_Signature 
  (Name       => "John G. Volan",
   Employer   => "Texas Instruments Advanced C3I Systems, San Jose, CA",
   Work_Email => "johnv@ti.com",
   Home_Email => "johnvolan@sprintmail.com",
   Slogan     => "Ada95: World's *FIRST* International-Standard OOPL",
   Disclaimer => "My employer never defined these opinions, so using " & 
                 "them would be totally erroneous...or is that just "  &
                 "nondeterministic behavior now? :-) ");
------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-05-30  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-05-29  0:00 Help with ADT Stack Jon Elbery
1997-05-29  0:00 ` Michael F Brenner
1997-05-29  0:00 ` Help With Ada_Io and Exceptions David H. Haley
1997-05-30  0:00   ` John G. Volan

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