Hello
I use turbo_pascal  language for learning. I write a program  (T3ch18.pas) 
which use two units – (Uadd_pro.pas) and  (Un3ch 18.pas).
In unit  Uadd_pro.pas , I used procedure ask. It works true when I test it 
only, but when I use them together,  above mentioned procedure in line – 
readLn- causes error and cpu abrupts running and finished running it.
If it is possible for you, please help me to solve this problem.
For run (T3ch18.pas) after running and choosing 1 for your choice in menu 
choice, it wants from you a positive- integer number as number of stuff and a 
real value as price of stuff and a name as vender, after then it ask you that 
if you  want to continue or not and in this stage error will occur.
Thanks for your attention.
 


      
program T3ch18;
{an answer for tamrine 3 of chapter 18.
 writer :Akbar Zamani date:1388/3/15}
uses uadd_pro,un3ch18,crt;
var myQueue :stuff_queue;
    answer :char;
    newstuff :stuff_data;
    chose :integer;
begin{T3ch18}
 clrscr;
 myQueue.Init;
 repeat
  writeLn ('1 for add to queue ');
  writeLn ('2 for pop ');
  writeLn ('3 for send cost for an order');
  writeLn ('4 for exit ');
  repeat
   write (' chose your action >');
   readLn (chose)
  until chose in [1..4];
  case chose  of
   1: begin{add}
        answer:='y';
        writeLn ('       adding new stuff to queue      ');
        while answer in ['y', 'Y'] do
         begin{while}
          myQueue.makestuff(newstuff);
          myQueue.push(newstuff);
          {myQueue.}ask('do you want continue adding new stuff>', answer)
         end{while}
        end;{add}
   2:  myQueue.pop(newstuff);
   3:  myQueue.send_cost(newstuff);
   4: {exit}
  end;{case}
 until chose= 4;
 writeLn (' sending an order');
end.{T3ch18}
unit un3ch18;
interface
uses crt;
type
 status =(s, o);
 stuff_data =record
              stuff_status :status;
              number :longInt;
              cost :real;
              vender :string[20]
             end;{stuff_data}
 stuff_pointer = ^stuff;
 stuff = record
          stuff_info:stuff_data;
          link :stuff_pointer
         end;{stuff}

 stuff_queue = object
               procedure Init;
               procedure push(newstuff{in}:stuff_data);
               procedure pop (var newstuff{out}:stuff_data);
               function getPrice :real;
               procedure display;
               {procedure ask(x}{in}{:string;var y_answer}{out}{:char);  }
               procedure send_cost(newstuff{in}:stuff_data);
               procedure makestuff(var newstuff{out}:stuff_data);
              private
               first, last :stuff_pointer ;
               procedure dodisplay (data{in}:stuff_data);
              end;{stuff_queue}

implementation
procedure stuff_queue.Init;
{}
begin{Init}
 first := nil;
 last := nil
end;{Init}

{procedure stuff_queue.ask(x}{in}{:string;var y_answer}{out}{:char);}
{var next_ch :char;  {next_char as answer}
{begin}{ask}
 {repeat
  write (x:length(x));
  readLn (next_ch)
 until next_ch in ['y', 'Y', 'n', 'N' ] ;
 y_answer := next_ch
end;}{ask}



procedure stuff_queue.dodisplay(data:stuff_data);
{for displaying content of data.
 pre :none.
 post:content of data is shown.}
begin{dodisplay}
 with data do
  begin{with}
   write (number:25);
   if stuff_status = s  then write (' status= s ':15)
   else if stuff_status = o then write (' status= o ':15)
   else writeln('      An error has been occured. ');
   write ('cost=',cost:10,'$') ;
   writeLn ('vender =',vender :20)
  end{with}
end;{dodisplay}



function stuff_queue.getPrice :real;
{for calculate price of a order.
 pre :stuff_queue is defined.
 post:price is product of this function and is defined.}
var p :stuff_pointer;
begin{getPrice}
 p:=first;
 if first =nil then writeLn ('error, queue is empty ')
 else getPrice := p^.stuff_info.number*p^.stuff_info.cost
end;{getprice}


procedure stuff_queue.send_cost(newstuff{in}:stuff_data);
{for making information and price of an order.
 pre : newstuff was defined.
 post: price and information about the stuff is displayed.}
begin{send_cost}
 with newstuff do
  begin{with}
   write ('status =', 'o',' ','number =',number,' ','cost =',cost);
   writeLn (' ','vender = ',vender);
   writeLn ('total price = ',getPrice)
  end{with}
end;{send_cost}




procedure stuff_queue.push(newstuff{in}:stuff_data);
{for adding a new stuff to a queue of stuffs.
 pre : newstuff was defined.
 post:newstuff is added to queue of stuffs.}
var  p :stuff_pointer;
begin{push}
 p^.link :=nil;
 p^.stuff_info :=newstuff;
 last^.link := p;
 last := p
end;{push}


procedure stuff_queue.pop (var newstuff{out}:stuff_data);
{for omit a stuff from a queue.
 pre : queue was defined.
 post: newstuff is defined and this stuff is omitted from queue.}
var p :stuff_pointer;
begin{pop}
 p:=first;
 newstuff := p^.stuff_info;
 first := first^.link;
 p^.link :=nil;
 if p<> nil then  dispose (p)
end;{pop}

procedure stuff_queue.makestuff(var newstuff{out}:stuff_data);
{for create a stuff}
begin{makestuff}
 with newstuff do
  begin{with}
   stuff_status := s;
   write ('number of stuff>');
   readLn (number);
   write ('cost of each stuff>');
   readLn (cost);
   write ('vender> ');
   readLn (vender)
  end{with}
end;{makestuff}


procedure stuff_queue.display;
{for display contents of a queue.
 pre : queue is defined.
 post:none.}
var p :stuff_pointer;
begin{display}
 p := first;
 while p<>nil do
  begin{while}
   dodisplay(p^.stuff_info);
   p:= p^.link
  end{while}
end;{display}

end.{un3ch18}
unit uadd_pro;
interface
uses crt;
procedure ask(x{in}:string;var y_answer{out}:char);
implementation

procedure ask(x{in}:string;var y_answer{out}:char);
var next_ch :char;  {next_char as answer}
begin{ask}
 repeat
  write (x:length(x));
  readLn (next_ch)
 until next_ch in ['y', 'Y', 'n', 'N' ] ;
 y_answer := next_ch
end;{ask}

end.{add_pro}
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to