I have problems with my pascal code.
When compiling it shows me the error: syntax : until expected but end found.
PROGRAM EvilGOTO;
Var rand,count,guess : integer;
LABEL correct, schleife, wrong, ende;
begin
Randomize;
count :=0;
repeat
schleife:
count := count+1;
rand := Random(10);
guess := Random(10);
if rand=guess
then
writeln('Correct')
end
else
writeln('False Guess')
goto schleife
until
count = 10;
end.
can you help me?
PROGRAM EvilGOTO;
Var rand,count,guess : integer;
begin
Randomize;
count :=0;
repeat
rand := Random(10);
guess := Random(10);
if rand=guess then
writeln('Correct')
else
begin
count := count+1;
writeln('False Guess');
end
until count = 10 or rand=guess;
end.
Related
I am trying to make a program that allow me to read a text file and then print it in the terminal.
I just put the simplified parts below so that you see how I think it should work.
My problem is that if for example I open the file a.txt then b.txt it works.
But when I want to open a.txt again, the program stops with an error 217. Same if I want to open another file name c.txt for example. I've spent days on this problem but I do not know where it comes from. I looked on the internet and erorr 217 seems to be related to a non-existing file ? but it is not the case for me...
The error seems to occur on the 'assign' function.
To clarify :
'key' is a Char,
'map' is a two dimension dynamic array of a Record Type.
Repeat
readln(key);
name := key +'.txt';
fileLoading(name, map, maxX, maxY);
Until key = 'l';
...
procedure fileLoading (name : String; var map : PPObjet; var maxX,maxY : Integer);
var
fichier : Text;
i, j : Integer;
chaine : String;
begin
if (FileExists(name)) then
begin
assign(fichier, name);
reset(fichier);
read(fichier,maxX);
readln(fichier,maxY);
if (maxX < 1) or (maxX > MAX) or (maxY < 1) or (maxY > MAX) then
begin
writeln('Tailles invalides');
halt();
end;
allocationTab(maxX, maxY, map);
while (not eof(fichier)) do
begin
for j := 1 to maxY do
begin
readln(fichier,chaine);
for i := 1 to maxX do
begin
case chaine[i] of
'0' : begin
map[j][i].solide := false;
map[j][i].nature := 'v';
map[j][i].valeur := chaine[i];
end;
'1' : begin
map[j][i].solide := true;
map[j][i].nature := 'm';
map[j][i].valeur := chaine[i];
end;
'2'..'9' : begin
map[j][i].solide := false;
map[j][i].nature := 's';
map[j][i].valeur := chaine[i];
end;
end;
end;
end;
end;
end
else
begin
writeln('Erreur le fichier n''existe pas');
halt();
end;
close(fichier);
end;
...
This is the first time I ask a question on stack overflow and I'm not really familiar with it, so I hope my problem is clear enough, as well as my english.
Thanks in advance for all the help you may bring.
try setting
filemode:=0;
before your assign/reset
How can I add a line to a file/template before a specific other line, if it doesn't exist?
For example for the following JS file, I have to make sure that there's dependencies.push(...) line between the ABOVE THIS LINE and BELOW THIS LINE comment-lines. If the dependencies.push(...) is not present, I have to add it before the BELOW THIS LINE comment-line:
(function(ng) {
var dependencies = [];
/*DO NOT MODIFY ABOVE THIS LINE!*/
dependencies.push("mxdfNewTransaction.controller.mxdfNewTransactionCtrl");
/*DO NOT MODIFY BELOW THIS LINE!*/
ng.module('prismApp.customizations', dependencies, null);
})(angular);
I have to also do the same thing with a similar HTML template file.
Thanks for your help.
You have to parse the file line-by-line to find the spot to insert your code.
Something like this:
function AddLineToTemplate(
FileName: string; StartLine, EndLine, AddLine: string): Boolean;
var
Lines: TArrayOfString;
Count, I, I2: Integer;
Line: string;
State: Integer;
begin
Result := True;
if not LoadStringsFromFile(FileName, Lines) then
begin
Log(Format('Error reading %s', [FileName]));
Result := False;
end
else
begin
State := 0;
Count := GetArrayLength(Lines);
for I := 0 to Count - 1 do
begin
Line := Trim(Lines[I]);
if (CompareText(Line, StartLine) = 0) then
begin
State := 1;
Log(Format('Start line found at %d', [I]));
end
else
if (State = 1) and (CompareText(Line, AddLine) = 0) then
begin
Log(Format('Line already present at %d', [I]));
State := 2;
break;
end
else
if (State = 1) and (CompareText(Line, EndLine) = 0) then
begin
Log(Format('End line found at %d, inserting', [I]));
SetArrayLength(Lines, Count + 1);
for I2 := Count - 1 downto I do
Lines[I2 + 1] := Lines[I2];
Lines[I] := AddLine;
State := 2;
if not SaveStringsToFile(FileName, Lines, False) then
begin
Log(Format('Error writing %s', [FileName]));
Result := False;
end
else
begin
Log(Format('Modifications saved to %s', [FileName]));
end;
break;
end;
end;
if Result and (State <> 2) then
begin
Log(Format('Spot to insert line was not found in %s', [FileName]));
Result := False;
end;
end;
end;
You can use it like this:
if AddLineToTemplate(
'C:\path\to\customizations.js',
'/*DO NOT MODIFY ABOVE THIS LINE!*/',
'/*DO NOT MODIFY BELOW THIS LINE!*/',
' dependencies.push("mxdfNewTransaction.controller.mxdfNewTransactionCtrl");') then
begin
Log('Success');
end
else
begin
Log('Failure');
end;
Beware of the LoadStringsFromFile and SaveStringsToFile limitations, when working with Unicode files. See Inno Setup Reading file in Ansi and Unicode encoding.
I've written a simple function which does log2 computation of any integer number and then "ceils" it to the next integer:
function log2ceil(intVal: integer) return natural is
variable i : natural;
variable bitCount : natural;
begin
i := (intVal - 1);
bitCount := 0;
while (i > 0) loop
bitCount := bitCount + 1;
i:=shiftRightInt(i,1);
end loop;
return bitCount;
end log2ceil;
function shiftRightInt(ValueToShift: integer; NrToShift:natural) return integer is
begin
return to_integer(shift_right(to_unsigned(ValueToShift, 32), NrToShift));
end shiftRightInt;
Works, fine. The problem is with the simulation. Every time I try to simulate it with a simple function call in a test bench:
stim: process
begin
wait for 10 ns;
log2ceil(3);
wait;
end process;
it gives me following error:
ERROR: [VRFC 10-1472] type error near log2ceil ; expected type void
Why does it expect type Void? And how can I avoid getting this error?
Your tool isn't giving a very helpful error message. The problem is that you are referencing a function in log2ceil that has not yet been declared. Given the order you define things, shiftRightInt isn't visible yet.
When I compile your code with Modelsim, I get:
`
(vcom-1136) Unknown identifier "shiftRightInt".
Edit: After including your process I got the error #user1155120 pointed out. In Modelsim:
No feasible entries for subprogram "log2ceil".
As user1155120 noted, you are calling a function in a procedure context. You need to assign the return value to something.
First, you can swap the order of declarations. I.e.:
function shiftRightInt(ValueToShift: integer; NrToShift:natural) return integer is
begin
return to_integer(shift_right(to_unsigned(ValueToShift, 32), NrToShift));
end shiftRightInt;
function log2ceil(intVal: integer) return natural is
variable i : natural;
variable bitCount : natural;
begin
i := (intVal - 1);
bitCount := 0;
while (i > 0) loop
bitCount := bitCount + 1;
i:=shiftRightInt(i,1);
end loop;
return bitCount;
end log2ceil;
The other is to define a "prototype" of shiftRightInt:
function shiftRightInt(ValueToShift: integer; NrToShift:natural) return integer;
function log2ceil(intVal: integer) return natural is
variable i : natural;
variable bitCount : natural;
begin
i := (intVal - 1);
bitCount := 0;
while (i > 0) loop
bitCount := bitCount + 1;
i:=shiftRightInt(i,1);
end loop;
return bitCount;
end log2ceil;
function shiftRightInt(ValueToShift: integer; NrToShift:natural) return integer is
begin
return to_integer(shift_right(to_unsigned(ValueToShift, 32), NrToShift));
end shiftRightInt;
The code:
stim: process
begin
wait for 10 ns;
log2ceil(3);
wait;
end process;
is describing a procedure call, but log2ceil is a function. Procedures have a return type of void, whereas your function returns an integer.
You need to provide a dummy variable to collect the return value:
stim: process
variable dummy : natural;
begin
wait for 10 ns;
dummy := log2ceil(3);
wait;
end process;
The error message is a bit cryptic, but correct!
I learning to make a program that gets data from a txt file and places it in arrays.
the following are its types :
type
ekspedisi = record
nmeksp : string; // Nama Ekspedisi
jlp : string; // Jenis layanan pengiriman
biaya : integer; // Biaya pengiriman per kg
lp : integer; // per hari
end;
ekspedisiku = record
nom : array [1..100] of ekspedisi;
end;
and a simple algorithm
procedure getDaftarEkspedisi(var kirim : ekspedisiku);
var
i,j,k : integer;
eksp : text;
init : string;
garis : array [1..100] of integer;
mark : string;
jeks : integer;
count : integer;
begin
assign(eksp,'ekspedisi.txt');
reset(eksp);
i := 0;
k := 1;
j := 1;
mark := '|';
jeks := 10;
writeln('Loading ekspedisi.. ');
while(not(eof(eksp))) do
begin
readln(eksp,init);
i := i + 1;
for j := 1 to length(init) do
begin
if init[j] = mark then
begin
garis[k] := j;
k := k + 1;
end;
end;
for i := 1 to jeks do
begin
count := ((i-1)*5);
kirim.nom[i].nmeksp := copy(init,garis[1+count] + 2,garis[2+count]-garis[1+count]-2);
kirim.nom[i].jlp := copy(init,garis[2+count] + 2,garis[3+count]-garis[2+count]-2);
val(copy(init,garis[3+count] + 2,garis[4+count]-garis[3+count]-2),kirim.nom[i].biaya);
val(copy(init,garis[4+count] + 2,garis[5+count]-garis[4+count]-2),kirim.nom[i].lp);
end;
close(kirim);
writeln('loading sukses.');
end;
end;
from that code, i get the following error
<166,13>Error: Call by var for arg no.1 has to match exactly : got "ekspedisiku" expected "Text"
curiously, line 166 is only
close(kirim);
any help is appreciated.
You need to pass the file handle to close, so:
close(kirim);
should be:
close(eksp);
It also looks like you're closing the file at the wrong place in your function. It should most likely be after the while loop, so you need to change:
close(kirim);
writeln('loading sukses.');
end;
end;
to:
end;
close(kirim);
writeln('loading sukses.');
end;
Note that this mistake probably happened because your identation is messed up - if you're careful with formatting your code properly then you won't be so likely to make this kind of error.
I can't understand what's going on here. Can you give me a hand? This is the problematic code:
While not EOF(Archi) do begin
index:= index + 1;
Read(Archi, Alumno[index]);
Promes[index] := (Alumno[index].nota1 + Alumno[index].nota2) / 2;
if Promes[index] >= 6 then begin
alguPromo := true;
PromosIndex := PromosIndex + 1;
Promos[PromosIndex]:= Alumno[index];
end;
else begin
if Promes[index] > 4 then cantiRecu:= cantiRecu + 1;
else begin
LibresIndex += 1;
Libres[LibresIndex] := Alumno[index];
end;
end;
end;
The compiler marks error in the line 10 of this code (else begin). The error is:
Fatal: Syntax error, ; expected but ELSE found.
If someone wants to tray compile here is the entire code: http://pastebin.com/dRg1Lguu
Note that in Pascal the semicolon is a separator, not a terminator. Sometimes this doesn't matter, but in some cases it does, particularly before an else. Your code should be:
while not EOF(Archi) do
begin
index:= index + 1;
Read(Archi, Alumno[index]);
Promes[index] := (Alumno[index].nota1 + Alumno[index].nota2) / 2;
if Promes[index] >= 6 then
begin
alguPromo := true;
PromosIndex := PromosIndex + 1;
Promos[PromosIndex] := Alumno[index]
end
else
begin
if Promes[index] > 4 then
cantiRecu:= cantiRecu + 1
else
begin
LibresIndex := LibresIndex + 1;
Libres[LibresIndex] := Alumno[index]
end
end
end
Note that I have re-formatted the code into a more conventional style which helps to make the program logic more easily understood and which also makes it more obvious where the semicolons are needed and where they are not.
Looks like problem in += operator