Quitting program in middle of program 
Author Message
 Quitting program in middle of program

I have Turbo Pascal 6.

I wish to be able to quit a program in the middle of a procedure which
normally accepts only numerical values. I would like the procedure to accept
the letter Q as well as a number. I have the following source code  -

Var Number: Real;
Repeat
   Write(' Enter a number or Q to quit .....  ');
   {$I-}   Read(Number)    {$I+}
Until IOresult=0

Could someone please suggest the addition code needed to Quit the program
when Q is entered. The final statement being something like "If Q='Q' then
QuitProgram.



Wed, 15 Dec 2004 21:51:01 GMT  
 Quitting program in middle of program
On Sat, 29 Jun 2002 13:51:01 +0000 (UTC), "Reg Edwards"

Quote:

>I have Turbo Pascal 6.

>I wish to be able to quit a program in the middle of a procedure which
>normally accepts only numerical values. I would like the procedure to accept
>the letter Q as well as a number. I have the following source code  -

>Var Number: Real;
>Repeat
>   Write(' Enter a number or Q to quit .....  ');
>   {$I-}   Read(Number)    {$I+}
>Until IOresult=0

>Could someone please suggest the addition code needed to Quit the program
>when Q is entered. The final statement being something like "If Q='Q' then
>QuitProgram.

Here is a skeleton program of one way:

Program ExitOnQ;
{Turbo v6.0}

VAR
exitFlag:Boolean;

Procedure EnterData(flg:Boolean);
Var Number: Real;
    Entry:String;
    err:Integer;
Begin
     Repeat
           Write('Enter a number or Q to quit ..... ');
           Readln(Entry);
           flg := Upcase(Entry[1]) = 'Q';
           If not flg then VAL(Entry, Number, err);
           If err = 0 then Writeln(Number) else Writeln('Try again');
     Until flg;
End;

BEGIN
     Writeln; Writeln;
     EnterData(exitFlag);
     If exitFlag then Exit;
END.

Var Number: Real;
Repeat
   Write(' Enter a number or Q to quit .....  ');
   {$I-}   Read(Number)    {$I+}
Until IOresult=0



Thu, 16 Dec 2004 00:18:01 GMT  
 Quitting program in middle of program
Cliff, thanks very much for your time and expertise. I've been trying on and
off for ages to get something to work. With  minor modifications it fits
very nicely in my main programs.
----
Reg
========================
Regards from Reg G4FGQ
Free Radio Modelling Software
Go to  http://www.g4fgq.com
========================


Thu, 16 Dec 2004 18:07:27 GMT  
 Quitting program in middle of program

Personally I would never use the  >    {$I-}   Read(Number)    {$I+}
and ioresult to decide whether you should quit or not. It means that any
mistype would lead to quitting even if the user of the program did not
intend to.

The way I solved this is
1 I wrote a little editor function that edits strings
    function editstring(s:string):string;
    arrow left/right = move cursor
    esc exits, the function returns the unmodified string
    enter accepts and the funtion returns the edited string
2 Two funtions (for the integer type and for the real type)
   the function passes the value of the parameter to the editstring
function as a string. The returned string is analysed with VAL. If the
returned string can't be interpreted the value of the parameter remains
unchanged.

here's some code. It's ripped from a larger package. But it gives an
idea.
You need a mouse unit for it, or strip all the mouse commands from the
code.

--Femme
-----------------------------------------
Program testedit;

uses crt,mouse;

Procedure piep;
begin
  sound(2000);
  delay(100);
  nosound;
end;

function decimals(x:extended;signif:integer):integer;
   {write(myreal:0:decimals(myreal,3)
    returns the number with only 3 significant digits
    e.g. 1.23   or 0.000123 }
var d:integer;
begin
   x:=abs(x);
   if x<1e-10 then d:=-1 else d:=-round(ln(x)/ln(10)-0.4999)+signif-1;
   if d<0 then decimals:=0 else decimals:=d;
end;

function i2s(i:longint):string;  {integer to string conversion}
var s:string;
begin
  str(i,s);
  i2s:=s;
end;

function R2S(r:extended;displdeci:integer):string;    {real to string}
var s:string;
begin
  str(r:0:decimals(r,displdeci),s);
  R2S:=s;
end;

const overwritemode:boolean=false;

function editstring(s:string):string;
var i,x,y:integer;
    ch:char;
    eerstekeer:boolean;
    dum:string[2];
    mb:word;
    mx,my:integer;
begin
  x:=wherex;
  y:=wherey;
  i:=1;
  eerstekeer:=true;
  editstring:=s;
  repeat
    gotoxy(x,y);
    write(s);
    clreol;
    gotoxy(x+i-1,y);
    if overwritemode then
    begin
      if i>length(s) then s[i]:=' ';
      write(s[i]);
      gotoxy(x+i-1,y);
    end;
    if mouseavailable then
    begin
      mouse_cursor(true);                         {show muis cursor}
      repeat until mousebutton=0;                 {wait till not mouse
clicked}
    end;

    repeat until (keypressed or mouse_clicked($FF)); {wait for mouse or
key}
    if mouseavailable then mouse_cursor(false);   {hide muis cursor}

    if keypressed then ch:=readkey else
    begin
      mx:=integer(mouse_clicked_x) div 8 +1-lo(windmin);
      my:=integer(mouse_clicked_y) div 8 +1-hi(windmin);
      if my<>y then ch:=#13 else
      begin
        ch:=#1;
        if mx<x then i:=1 else
          if (mx>=x+length(s)) then i:=length(s)+1 else i:= mx-x+1;
      end;
    end;

     case ch of
      #1 :;
      #27: exit;
      #13:begin
            editstring:=s;
            writeln;
            exit;
          end;
      #8 :if i>1 then begin                {backspace}
                        delete(s,i-1,1);
                        dec(i);
                      end;
      #32..#254:begin
                  if eerstekeer then s:='';
                  if (i>length(s))
                    then s:=s+ch
                    else if overwritemode then s[i]:=ch
                                        else  insert(ch,s,i);
                  inc(i);
                end;
      #17: if i>1 then dec(i);                 {Touch <}
      #26: if i<=length(s) then inc(i);        {Touch >}
      #18: if i<=length(s) then delete(s,i,1); {Touch del}
      #0:begin
           ch:=readkey;
           case ch of
     {<}     #75: if i>1 then dec(i);
     {>}     #77: if i<=length(s) then inc(i);
     {del}   #83: if i<=length(s) then delete(s,i,1);
     {ins}   #82: overwritemode := not overwritemode;
              else piep;
           end
         end;
    end;
    eerstekeer:=false;
  until false;
end;

function editinteger(i:longint):longint;
var s:string;
    Iedit:longint;
    code:integer;
begin
  editinteger:=i;
  s:=editstring(i2s(i));
  val(s,Iedit,code);
  if (code=0)
     then editinteger:=iedit
     else writeln(' Can''t interpret ',S);
end;

function editreal(r:extended;displdeci:integer):extended;
var s:string;
    redit:extended;
    code:integer;
begin
  editreal:=r;
  str(r:0:decimals(r,displdeci+4),s);
  s:=editstring(R2S(r,displdeci));
  val(s,redit,code);
  if (code=0)
     then editreal:=redit
     else  writeln(' Can''t interpret ',S);
end;

const i:integer=123;
      r:double=456.789;
begin
  write('I=');
  i:=editinteger(i);
  writeln('I=',i);

  write('R=');
  R:=editreal(R,8);
  writeln('R=',r:0:6);

end.



Fri, 17 Dec 2004 07:53:40 GMT  
 Quitting program in middle of program

Quote:
>I have Turbo Pascal 6.

>I wish to be able to quit a program in the middle of a procedure which
>normally accepts only numerical values. I would like the procedure to accept
>the letter Q as well as a number. I have the following source code  -

>Var Number: Real;
>Repeat
>   Write(' Enter a number or Q to quit .....  ');
>   {$I-}   Read(Number)    {$I+}
>Until IOresult=0

>Could someone please suggest the addition code needed to Quit the program
>when Q is entered. The final statement being something like "If Q='Q' then
>QuitProgram.

    The accepting of a letter into a floating point variable is not a good idea
because it leaves you with no way to check if that letter was in fact a 'Q'.
A better way is to retrieve a string from the user, test/process for 'Q' then
convert that string into floating point using the 'val' function after t{*filter*}
leading/trailing blanks.

  To quit in middle of program, although, like 'goto', its use is not good 'style',
the 'halt' procedure can do that for you (even in middle of a loop or nested
procedures).  It takes an optional parameter for the return code.
Caution: because this command is designed as an emergency exit, it might
bypass exit procedures in some compiler versions.



Sat, 18 Dec 2004 00:00:06 GMT  
 Quitting program in middle of program

Quote:

> Here is a skeleton program of one way:

> Program ExitOnQ;
> {Turbo v6.0}

> VAR
> exitFlag:Boolean;

> Procedure EnterData(flg:Boolean);
> Var Number: Real;
>     Entry:String;
>     err:Integer;
> Begin
>      Repeat
>            Write('Enter a number or Q to quit ..... ');
>            Readln(Entry);
>            flg := Upcase(Entry[1]) = 'Q';

There is a bug in that line. You do not test for an empty string.

flg:= (entry<>'') and (Upcase(Entry[1]='Q');

Quote:
>            If not flg then VAL(Entry, Number, err);
>            If err = 0 then Writeln(Number) else Writeln('Try again');
>      Until flg;
> End;

> BEGIN
>      Writeln; Writeln;
>      EnterData(exitFlag);
>      If exitFlag then Exit;
> END.

Osmo


Wed, 22 Dec 2004 19:48:04 GMT  
 Quitting program in middle of program

Quote:



>> Here is a skeleton program of one way:

>> Program ExitOnQ;
>> {Turbo v6.0}

>> VAR
>> exitFlag:Boolean;

>> Procedure EnterData(flg:Boolean);
>> Var Number: Real;
>>     Entry:String;
>>     err:Integer;
>> Begin
>>      Repeat
>>            Write('Enter a number or Q to quit ..... '); Readln(Entry);
>>            flg := Upcase(Entry[1]) = 'Q';

> There is a bug in that line. You do not test for an empty string.

> flg:= (entry<>'') and (Upcase(Entry[1]='Q');

THis doesn't seem right either..
if you enter an empty string the flg wouldn't be set

flg := (entry<>'') OR (Upcase(entry[1]='Q');

Quote:

>>            If not flg then VAL(Entry, Number, err); If err = 0 then
>>            Writeln(Number) else Writeln('Try again');
>>      Until flg;
>> End;

>> BEGIN
>>      Writeln; Writeln;
>>      EnterData(exitFlag);
>>      If exitFlag then Exit;
>> END.

> Osmo

Bas


Thu, 23 Dec 2004 00:56:05 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Quitting a D3 DB program

2. Quit choice for a program

3. Quit choice for a program

4. Running sub-programs in TP6/TP7 programs

5. Need HELP FINISHING PROGRAM (posted program) PASCAL

6. Execute DOS-command/program from a program.

7. Execute DOS-command/program from a program.

8. linking pascal program into c program?!

9. Help needed (running a program from a program).

10. M353 Programming and Programming Languages

11. Make a program that runs a program

12. How run a program.exe at the end of a pascal program

 

 
Powered by phpBB® Forum Software