Forum > Beginners

Following program won't compile, need help

(1/2) > >>

jojoni6:
Hello everyone,

first and foremost i must say i am new to pascal, and working with the 2.4.0 FPC version because it is the version requested by the course i am taking.
I have tried to build the following program, but the compiler won't work at all - it keeps sending me the dreaded "Fatal: Compilation Aborted" message and i have no idea why.
I have been fiddling with it for a the better part of two hours now and i finally gave up, so i request the help of the good people here at the forum - please help me with this problem!

the program is as following:

--------------------------------------------
program hw1;
var counter, first, second, third, fourth : integer;
var sum1, sum2, ans : integer;
var tnai : boolean;

function powerOfThree(x : integer) : integer;
        begin
                powerOfThree := x*x*x;
        end;

function isTaxiCab(w, x, y, z : integer) : boolean;
        begin
                isTaxiCab := (w = y) or (x = z);
        end;

begin

second := 1;
counter :=0;
tnai := false;
ans := 0;

while not (tnai) do
begin
        fourth :=1;
        sum1 :=0;
        for first := 1 to second do
        begin
                sum2 :=0;
                sum1 := powerOfThree(first)+powerOfThree(second);
                for third := 1 to fourth do
                begin
                       while sum2<sum1 do
                       begin
                           sum2 := powerOfThree(third)+powerOfThree(fourth);
                           counter := counter+1;
                       end;

                       if sum2=sum1
                       then tnai := isTaxiCab(first,second,third,fourth);
                       if tnai then
                       begin
                                ans := sum1;
                                Goto print;
                       end;
                       fourth := fourth+1;
                       counter := counter+1;
                end;
        counter := counter+1;
        end;
second := second+1;
counter := counter+1;
end;

print:
WriteLn(ans);
WriteLn(counter);

end.


--------------------------------------------

this program is supposed to calculate the first taxi number and count all repetitions of loops in the code, by the way. please do not comment on the algorithm or bugs in the code that do not have anything to do with the compilation error, i haven't had a chance to check it myself since, well - the compiler isn't working.

thanks a lot in advance to anyone who will spend his time helping me!

skalogryz:

--- Quote from: jojoni6 on March 27, 2016, 07:24:02 pm ---I have tried to build the following program, but the compiler won't work at all - it keeps sending me the dreaded "Fatal: Compilation Aborted" message and i have no idea why.

--- End quote ---
it actually sends more dreaded messages such as:

--- Code: ---Compiling test.pas
test.pas(44,38) Error: Identifier not found "print"
test.pas(44,43) Error: identifier isn't a label
test.pas(55,6) Error: Identifier not found "print"
test.pas(55,6) Fatal: Syntax error, ";" expected but ":" found
Fatal: Compilation aborted

--- End code ---
That suggests that you're missing declaration of "print" label, that is to be used in goto operation.


Just add the line

--- Code: ---label print;

--- End code ---
right after the line "var tnai : boolean;" and you should be fine.

jojoni6:

--- Quote from: skalogryz on March 27, 2016, 07:30:23 pm ---
--- Quote from: jojoni6 on March 27, 2016, 07:24:02 pm ---I have tried to build the following program, but the compiler won't work at all - it keeps sending me the dreaded "Fatal: Compilation Aborted" message and i have no idea why.

--- End quote ---
it actually sends more dreaded messages such as:

--- Code: ---Compiling test.pas
test.pas(44,38) Error: Identifier not found "print"
test.pas(44,43) Error: identifier isn't a label
test.pas(55,6) Error: Identifier not found "print"
test.pas(55,6) Fatal: Syntax error, ";" expected but ":" found
Fatal: Compilation aborted

--- End code ---
That suggests that you're missing declaration of "print" label, that is to be used in goto operation.


Just add the line

--- Code: ---label print;

--- End code ---
right after the line "var tnai : boolean;" and you should be fine.



--- End quote ---

how odd, it doesn't show any of the other messages to me... adding the label declaration didn't help, keeps saying i have a fatal error somewhere. 

Bart:
Compiles fine with the suggested fix from skalogryz:


--- Code: ---C:\Users\Bart\LazarusProjecten\ConsoleProjecten\bugs\Forum\Taxicab>fpc taxicab.lpr
Free Pascal Compiler version 3.0.0 [2015/11/16] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling taxicab.lpr
Linking taxicab.exe
60 lines compiled, 0.1 sec, 26304 bytes code, 1252 bytes data

C:\Users\Bart\LazarusProjecten\ConsoleProjecten\bugs\Forum\Taxicab>taxicab
2
1

--- End code ---

2 is of course the most trivial answer to the question...
Bart

skalogryz:

--- Quote from: jojoni6 on March 27, 2016, 07:35:25 pm ---how odd, it doesn't show any of the other messages to me... adding the label declaration didn't help, keeps saying i have a fatal error somewhere.

--- End quote ---
That's highly likely that it keeps saying you fatal error.

Try the following code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program hw1;var counter, first, second, third, fourth : integer;var sum1, sum2, ans : integer;var tnai : boolean;label print; function powerOfThree(x : integer) : integer;        begin                powerOfThree := x*x*x;        end; function isTaxiCab(w, x, y, z : integer) : boolean;        begin                isTaxiCab := (w = y) or (x = z);        end; begin second := 1;counter :=0;tnai := false;ans := 0; while not (tnai) dobegin        fourth :=1;        sum1 :=0;        for first := 1 to second do        begin                sum2 :=0;                sum1 := powerOfThree(first)+powerOfThree(second);                for third := 1 to fourth do                begin                       while sum2<sum1 do                       begin                           sum2 := powerOfThree(third)+powerOfThree(fourth);                           counter := counter+1;                       end;                        if sum2=sum1                       then tnai := isTaxiCab(first,second,third,fourth);                       if tnai then                       begin                                ans := sum1;                                Goto print;                       end;                       fourth := fourth+1;                       counter := counter+1;                end;        counter := counter+1;        end;second := second+1;counter := counter+1;end; print:WriteLn(ans);WriteLn(counter); end. 
Could you please also send a screenshot of what you've actually seeing on the screen? It might be that your IDE settings do not allow the use of goto operator.

Navigation

[0] Message Index

[#] Next page

Go to full version