Problem with Procedure and Case

2

I want to make a menu, it's for a game, to try the case and procedure , but it does not work. This is the code:

Program Juego;
uses crt;

Procedure Menu;
var op:integer;
begin
    Writeln ('Seleccione una opcion') ;
    Writeln ('1-Informacion');
    Writeln ('2-Salir');
    Read(op);
    Case op of  
        1:Procedure Informacion ;
        2:Procedure Salir ;
        else Writeln('Seleccione una opcion del 1 al 2');
    end;
end;

Procedure Informacion;
begin
    Writeln('Desarrollado por:');
    Readln();
end;

Procedure Salir;
var s:integer;
begin
    Writeln('¿Esta seguro que desea salir?');
    Writeln('1-SI');
    Writeln('2-NO');
    Read(s);
    If s=1 then
        Halt(0);
    If s=0 then 
        Procedure Menu;
end;

begin
    Procedure Menu;
end.

This is the error you give me:

Juego.pas(11,13) Error: Illegal expression
Juego.pas(12,2) Error: Constant Expression expected
Juego.pas(12,2) Fatal: Syntax error, ":" expected but "ordinal const" found
Fatal: Compilation aborted
    
asked by Enzo 17.07.2017 в 01:23
source

1 answer

2

I have not used Pascal for 17 years, but if I remember correctly, the code has (at least) two problems:

  • When you call a procedure, it is not necessary to indicate that it is a procedure, that is only done in the definition of it. So, instead of doing Procedure Informacion or Procedure Salir , you should do Informacion() and Salir() respectively:

    Case op of  
        1: Informacion();
        2: Salir();
        else Writeln('Seleccione una opcion del 1 al 2');
    end;
    

    And the same for Menu() in the main.

  • You can not call a function / procedure that has not been defined before. In Menu you are calling Informacion and Salir that are not defined until later. The solution could be to move Informacion and Salir to be above Menu .

    But there is a problem: from Menu llamas to Salir and from Salir llamas to Menu ... With which, it is not enough to simply move the position functions. You have to declare the functions in advance ( forward declaration ), this is done by setting the header of the function with the keyword forward after, like this:

    procedure Informacion; forward;
    procedure Salir; forward;
    

    You could also do it for Menu, but since the Menu procedure is going to be defined first, it's not necessary.

  • Applying those changes, the code stays that way and it works ( as you can see here ):

    Program Juego;
    uses crt;
    
    procedure Informacion; forward;
    procedure Salir; forward;
    
    Procedure Menu;
    var op:integer;
    begin
        Writeln ('Seleccione una opcion') ;
        Writeln ('1-Informacion');
        Writeln ('2-Salir');
        Read(op);
        Case op of  
            1: Informacion();
            2: Salir();
            else Writeln('Seleccione una opcion del 1 al 2');
        end;
    end;
    
    Procedure Informacion;
    begin
        Writeln('Desarrollado por:');
        Readln();
    end;
    
    Procedure Salir;
    var s:integer;
    begin
        Writeln('¿Esta seguro que desea salir?');
        Writeln('1-SI');
        Writeln('2-NO');
        Read(s);
        If s=1 then
            Halt(0);
        If s=0 then 
            Menu();
    end;
    
    
    begin
        Menu();
    end.
    
        
    answered by 17.07.2017 в 02:12