从 Pascal 过程中完全退出程序

Quit program completely from a Pascal procedure

我的Pascal程序在mainbegin语句中有一个菜单和4个程序。在每个过程中,我都会向用户确认他们是否想 return 到菜单,否则程序将退出,但是每次程序要退出时,它都会再次 return 到菜单。

procedure quit;
begin
  writeln('<Enter> to quit...');
  readln;
end

procedure error;
begin
  writeln('Error. Try Again...');
  readln;
end;

procedure option1;
begin
  clrscr;
  writeln('this is option 1');
  writeln('would you like to continue? (y/n)');
  readln(confirm);
  if confirm = 'y' then
  begin 
    writeln('something will happen...');
  end;

  if confirm = 'n' then
    begin
      writeln('Return to main menu ? (y/n)');
      readln(option);
      if option = 'y' then
        exit
      else
        quit;
    end;  
end;

procedure option2;
begin
  clrscr; 
  writeln('this is option2');
  writeln('would you like to continue? (y/n)');
  readln(confirm);
  if confirm = 'y' then
  begin 
    writeln('something will happen...');
  end;

  if confirm = 'n' then
    begin
      writeln('Return to main menu ? (y/n)');
      readln(option);
      if option = 'y' then
        exit
      else
        quit;
    end; 
end;

主要开始语句:

begin
  repeat
    1: clrscr;
    writeln('Pascal Menu');
    gotoxy(4, 3);
    writeln('1. Option 1');
    gotoxy(4, 4);
    writeln('2. Option 2');
    gotoxy(4, 5);
    writeln('0. Quit Program');
    readln(choice);

    if choice > 2 then
    begin
      error
    end;

  case choice of
    1: option1;
    2: option2;
    0: quit;
  end;
  until choice = 0;

  exit;           
end.

我对 Pascal 比较陌生,非常感谢任何帮助。

我使用的一种方法是在 quit 过程中将 0 分配给 choice 变量。这样,当程序回到主 begin 语句时,满足 until 条件并跳出循环。允许程序退出。

procedure quit;
begin
  writeln('<Enter> to quit...');
  readln;
  choice := 0;
end

调用 halt 并传递所需的退出代码:

halt(0);

如果省略退出代码,则使用默认值 0

halt;