Search code examples
pascalfreepascallazarus

Quit program completely from a Pascal procedure


My Pascal program has a menu in the main begin statement and 4 procedures. Within each procedure I am confirming to the user if they would like to return to the menu else the program will quit, however every time the program is meant to quit, it returns to the menu again.

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;

Main begin statement:

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.

I'm relatively new to Pascal and any help would be greatly appreciated.


Solution

  • Call halt passing the required exit code:

    halt(0);
    

    If you omit the exit code, then a default value of 0 is used:

    halt;