0
Program prueba1;
uses Estructu;
Var Pila1:Pila; Fila1,Completa:Fila;
Begin
    Inicfila (Completa);
    readpila(Pila1);
    readfila(Fila1);
    While (not pilavacia(Pila1) and not filavacia(Fila1)) do
    begin
        if (tope(Pila1) > primero(Fila1)) then
        begin
            agregar(Completa, desapilar(Pila1))
        else
            if (tope(Pila1) < primero(Fila1)) then
            begin
                agregar(Completa, extraer(Fila1))
            else
                if (tope(Pila1) = primero(Fila1)) then
                begin
                    agregar(Completa, desapilar(Pila1));
                    agregar(Completa, extraer(Fila1))
                end
            end
        end
    end
    write('El resultado final de Completa es');
    Writefila(Completa);
End.

The purpose of the program would be to organise in Completa all the variables from Pila1 and Fila1 in order from first to last. I don't know what I'm doing wrong and would appreciate the help

  • 1
    You may find it helpful to read [this answer](https://stackoverflow.com/a/28221465/62576) I posted that provides a tutorial on the proper use of `begin` and `end` in Pascal. – Ken White May 03 '20 at 20:52

1 Answers1

1

You are not using if ... then ... else and begin ... end correctly.

There is the concept of a block, which is started with begin and ended with end. Anywhere where a single statement is expected, you can put a block instead. This is also the case for if <condition> then <statement> else <statement>;.

So, this code is valid:

if something() then
  stuff
else
  stuff;

...as is this:

if something() then
  begin
    stuff;
    moreStuff;
  end
else
  begin
    otherStuff;
    moreOtherStuff;
  end;

However, this (which you are using) is not:

if something() then
  begin
    stuff // I guess here you omitted the semicolon because you correctly remembered
          // that there shouldn't be a semicolon before `else`, but...
else // WRONG, this is in the middle of the block!
    otherStuff;
  end;

To see why, let's fix the indention to match the logical interpretation of this code:

if something() then
  begin
    stuff
    else // ????????
    otherStuff;
  end;

You get an error since begin ... else ... end is not a valid construct. Since there is a begin but then no end before the else, your else is in the middle of the then block which makes no sense.

Make sure to end your block before starting the else part, and then begin a new block.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • My thanks though now I'm having problems with the write and writefila lines – Sebastiàn Cappi May 03 '20 at 19:37
  • I think you are missing the `;` after the condition ended. So at the `end` that concludes the `if` or `while`, you need a `;` (**not** before the `else` though). – CherryDT May 03 '20 at 19:38
  • it would be also nice to omit ;'s after the last statements in begin end. Thin about it as begin and end are brackets and you are using ; to separate elements in brackets. You do not need ; after the last element in begin end. Many compilers will tolerate it or even insert empty statement after last ; – robert aleksic Jun 14 '20 at 12:46