Code not moving past Read - pascal

I'm having a problem with my code. It starts fine but when asking to get input from the user at the start it will not move onto the if statement. How can I fix this. I've tried heaps to get around this and eventually got it to read data but continually say that it is invalid.
PROGRAM AT3 (input, output);
uses crt, math;
CONST
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
VAR
Studname : array of string;
studmark : array of integer;
flag : boolean;
studinfo : text;
input : string;
count : integer;
num : integer;
input2: integer;
highmark, lowmark : integer;
median, average : integer;
BEGIN
lowmark := 100;
highmark := 0;
median := 0;
ASSIGN (Studinfo, 'ExamResults.txt');
flag := false;
WRITELN('welcome to the Band generator.');
WRITELN('To enter student results, please enter the number of students. To see class statistics, please type zzz. To clear the screen type clear screen. And to exit the program, type exit');
While flag = false DO
ReadLN (input);
IF input = 'zzz' THEN
WHILE not EOF(Studinfo) DO
BEGIN
WRITELN(studinfo);
END;
IF input = 'exit' THEN
Flag := true;
IF input = 'clear screen' THEN
CLRSCR
ELSE
if input2 <> 0 THEN
num := input2
ELSE
WRITELN('Please enter a valid number.');
FOR count := 0 to num-1 DO
BEGIN
WRITELN('Please enter name of student', count);
read(studname[count]);
WRITE(studinfo, studname[count]);
WRITELN('Please enter mark of student', count, 'out of 100 (nearest whole number)');
read(studmark[count]); write(studinfo, studmark[count]);
IF studmark[count] >=band6 THEN
WRITELN(studinfo, 'band6');
IF studmark[count] >=band5 THEN
WRITELN(studinfo, 'band5');
IF studmark[count] >=band4 THEN
WRITELN(studinfo, 'band4');
IF studmark[count] >=band3 THEN
WRITELN(studinfo, 'band3');
IF studmark[count] >=band2 THEN
WRITELN(studinfo, 'band2');
IF studmark[count] <band2 THEN
WRITELN(studinfo, 'band1');
IF studmark[count] >= highmark THEN
highmark := studmark[count];
IF studmark[count] <= lowmark THEN
lowmark := studmark[count];
END;
median := highmark MOD 2;
CLOSE(studinfo);
END.

Questions like this often attract a comment like "SO is not a homework-doing
service." This q is a bit exceptional because there is no polite way I can think
of to describe it. It has all the signs of having been written by someone who
doesn't really know what they are doing - but don't despair, we were all beginners once!
I'm not going to rewrite all your code for you - because you won't learn anything from that - but the following should at least give
you a functional main loop which you can then complete yourself and embellish as necessary.
The main thing I've fixed is the sequence of actions in the loop - the original
was a complete muddle, to put it mildly. I've added various comments enclosed in braces {}.
There is a more recent style of comments that uses // but I don't know what flavour
of Pascal you're using.
Code:
const
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
ArraySize = 20; { to set explicit array sizes }
var
studname : array[1..ArraySize] of string;
studmark : array[1..ArraySize] of integer;
flag : boolean;
studinfo : text;
kbdinput : string; { to avoid name clash with program Input param}
count : integer;
num : integer;
{input2: integer; not used}
highmark,
lowmark : integer;
median, average : integer;
begin
lowmark := 100;
highmark := 0;
median := 0;
assign (studinfo, 'c:\temp\examresults.txt'); { always use full path for file name }
Rewrite(studinfo); { set studinfo in the state to allow writing to the file}
flag := false;
writeln('welcome to the band generator.');
writeln('to enter student results, please enter the number of students when prompted.');
while flag = false do
begin
write('please enter a valid number of students. ');
readln(num);
for count := 1 to num do { not change of start and stop values}
begin
write('please enter name of student #', count, ' followed by [Enter] ');
readln(studname[count]);
write(studinfo, studname[count]);
write('please enter mark of student #', count, ' out of 100 (nearest whole number) followed by [Enter] ');
readln(studmark[count]);
write(studinfo, studmark[count]);
end;
{
if studmark[count] >=band6 then
writeln(studinfo, 'band6');
if studmark[count] >=band5 then
writeln(studinfo, 'band5');
if studmark[count] >=band4 then
writeln(studinfo, 'band4');
if studmark[count] >=band3 then
writeln(studinfo, 'band3');
if studmark[count] >=band2 then
writeln(studinfo, 'band2');
if studmark[count] <band2 then
writeln(studinfo, 'band1');
if studmark[count] >= highmark then
highmark := studmark[count];
if studmark[count] <= lowmark then
lowmark := studmark[count];
end;
median := highmark mod 2;
}
writeln('to see class statistics, please type zzz. to clear the screen type zzz and to exit the program, type exit');
readln (kbdinput);
if kbdinput = 'zzz' then
{ The following does nothing useful
while not eof(studinfo) do
begin
writeln(studinfo);
end;
}
;
if kbdinput = 'exit' then
flag := true
else
if kbdinput = 'clear screen' then
{clrscr;'}
close(studinfo);
end;
end.

Look at your line:
While flag = false DO
ReadLN (input);
Flag is never false so it reads forever. You are having problems with your begin/end blocks.

Related

Is it possible to open a Text File in Pascal several times in the same program?

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

Read / write different Records data from / to Untyped files in Pascal?

I've a programming project in my college.
Using a File type for storing data is allowed, and I did exactly like this one: pascal-programming
And, here's what I achieved so far:
I tried to write the Records data into Untyped files instead and it worked
I want to override a function with dynamic parameter (e.g: I can switch which Record I want to process, in this case there's 2 different "Records").
Open(var f: File; var data)
data = represent can receive "anything". cmiiw
The reason why I did this, I don't think it's a good idea to recreate the same function over and over, e.g: when using 3 or more different "Records"
I also encountered a problem that the files can't store or backup the actual binary files to the temporary "Records" variable, it always give the 0 values.
go to my github source code
my solution here doesn't provide any generic related procedures (check the last sentences):
program test_untyped;
{ A crude database recording }
uses crt;
type
Temployee = record
name : string[20];
address : string[40];
phone : string[15];
age : byte;
salary : longint;
end;
arr_employee = array[1..100] of Temployee;
var
F : File;
c : char;
// r : Temployee;
r, realR : arr_employee;
s : string;
i, j, n : integer;
procedure fRead;
begin
seek(F, 0);
i := 0;
repeat
clrscr;
inc(i);
writeln('increment: ', i); readln;
writeln('File position : ',filepos(F));
blockRead(F, r[i], sizeOf(Temployee));
writeln('Name = ', r[i].name); { Input data }
writeln('Address = ', r[i].address);
writeln('Phone = ', r[i].phone);
writeln('Age = ', r[i].age);
writeln('Salary = ', r[i].salary);
write('Show data again (Y/N) ?');
repeat
c:=upcase(readkey); { Ask user : Input again or not }
until c in ['Y','N'];
writeln(c);
// realR[i] := r[i]; // backup, to show later
until c='N';
end; // end fRead
procedure fWrite;
begin
seek(F, filesize(F));
repeat
clrscr;
inc(i);
writeln('berapa nilai i: ', i);
writeln('File position : ',filepos(F));
write('Name = '); readln(r[i].name); { Input data }
write('Address = '); readln(r[i].address);
write('Phone = '); readln(r[i].phone);
write('Age = '); readln(r[i].age);
write('Salary = '); readln(r[i].salary);
blockWrite(F, r[i], sizeOf(Temployee)); { Write data to file }
write('Input data again (Y/N) ?');
repeat
c:=upcase(readkey); { Ask user : Input again or not }
until c in ['Y','N'];
writeln(c);
until c='N';
end;
// procedure fDelete;
// var
// nama: string;
// delElement: integer;
// tempR: Temployee;
// begin
// seek(F, 0);
// write('search your data by name: '); readln(nama);
// while not eof(F) do
// begin
// writeln('file position: ', filePos(F));
// blockRead(F, tempR, sizeOf(Temployee));
// if (nama = tempR.name) then
// begin
// delElement := filePos(F);
// end else
// begin
// // seek(F, )
// blockWrite(F, tempR, sizeOf(Temployee));
// end;
// end;
// end; // end fDelete
procedure fDisplay;
begin
writeln('nilai i saat ini: ', i); readln;
for j := 1 to i do
begin
clrscr;
writeln('Name = ', r[j].name); { Input data }
writeln('Address = ', r[j].address);
writeln('Phone = ', r[j].phone);
writeln('Age = ', r[j].age);
writeln('Salary = ', r[j].salary);
readln;
end;
end;
begin
clrscr;
// write('Input file name to record databases : '); readln(s);
s := 'coba1.dat';
assign(F,s); { Associate it }
{$I-}
reset(F, sizeOf(Temployee)); { First, open it }
{$I+}
n:=IOResult;
if n<>0 then { If it's doesn't exist then }
begin
{$I-}
rewrite(F, sizeOf(Temployee)); { Create it }
{$I+}
n:=IOResult;
if n<>0 then
begin
writeln('Error creating file !'); halt;
end;
end
else
begin { If it exists then }
n:=filesize(F); { Calculate total record }
// seek(F,n); { Move file pointer PAST the last record }
end;
fileMode := 2;
reset(F, sizeOf(Temployee));
fRead;
fWrite;
// fDelete;
fDisplay;
close(F);
end.
I'm wondering is the Pascal can be any good to use a generic programming? at least for this semester using Pascal in my college XD
Thank you and Best Regards,
EDIT:
Pascal still doesn't support Generic Programming 'till the day I posted this question. So sad, really.
You might wanna consider read this references instead.
I don't understand the main issue here, but would suggest using a typed file instead of an untyped one.
An untyped file is much harder to maintain, and provides (in my eyes) no benefits.
Consider the code:
type
Temployee = record
name : string[20];
address : string[40];
phone : string[15];
age : byte;
salary : longint;
end;
VAR
fEmployee : File Of Temployee;
Employees : ARRAY[0..100] Of Temployee;
Employee : Temployee;
PROCEDURE OpenEmployeeFile(CONST TheFileName:AnsiString);
BEGIN
AssignFile(fEmployee,TheFileName);
IF FileExistsUTF8(TheFileName) { *Converted from FileExists* }
THEN Reset(fEmployee)
ELSE Rewrite(fEmployee);
END;
PROCEDURE CloseEmployeeFile;
BEGIN
Close(fEmployee);
END;
FUNCTION ReadEmployee(Position:WORD): Temployee;
BEGIN
Seek(fEmployee,Position);
Read(fEmployee,Result);
END;
PROCEDURE WriteEmployee(CONST Employee:Temployee; Position:WORD);
BEGIN
Seek(fEmployee,Position);
Write(fEmployee,Employee);
END;
Error handling not implemented.
Code samples as a guideline, not complete.
It provides a basic skeleton for opening and closing the employee-file, as well as reading and writing at specific positions (specific records) in the file.
Open file.
Write all the records you want.
Close file.
Or.
Open file.
Read all the records you want.
Close file.

Pascal error 'call by var for arg no.1 has to match exactly'

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.

PASCAL Error: Until expected but end found

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.

Lazarus display numbers from memo to for exampel lisbox

I have these data:
CMD210 STA_ 99.0 uS Temp 22.1 C
CMD210 STAB 99.9 uS Temp 22 C
CMD210 STAB 0.1 mS Temp 22.1 C
CMD210 STA_ 99.5 uS Temp 22.1 C
CMD210 STAB 99.4 uS Temp 22 C
CMD210 ST__ 99.0 uS Temp 22.2 C
CMD210 STAB 0.1 mS Temp 22 C
CMD210 STAB 99.3 uS Temp 22.2 C
I would like to have a program that display the temperature from memo for exampel in a listbox.
I know I have to get loop and something with 2 char with 'p' and 'c', because the number is between those to letters....
procedure TForm1.Button4Click(Sender: TObject);
var
midlet,midler:char;
resultat,x:integer;
linecount,index:integer;
found: boolean;
begin
midlet:= 'p';
midler:='C';
index:=0;
resultat:=midlet+x+midler
found := false;
linecount := Memo1.lines.count;
while index<= linecount - 1 do
begin
if x = memo1.lines[index] then
found := true;
index :=index + 1;
end
if found = true then
ListBox1.text:= floattostrF(x,ffFixed,15,2);
end;
There are several problems in your example so this answer will be limited to "how extracting and converting the temperature from a line". You have fundamentally two ways to achieve the task:
use the regular expressions.
write a custom parser.
the custom parser is quite easy to write:
accumulate non-blank chars in an identifier.
if the identifier is equal to Temp then define a flag.
convert the identifier to a double if the flag is defined and if someting's been accumulated.
example:
program Project1;
uses
sysutils;
const
line1 = 'CMD210 STAB 99.3 uS Temp 22.2 C';
line2 = 'CMD210 STAB 0.1 mS Temp 22 C';
line3 = 'it is quite hot over there Temp 55.123456 C';
line4 = 'bla bla bla bla 12.564 C';
line5 = '';
function getTemperature(aLine: string): double;
var
reader: PChar;
identifier: string;
AccumulateTemp: boolean;
const
_Nan = 0/0;
begin
// initialize local variables.
identifier := '';
AccumulateTemp := false;
// success can be tested with isNan()
result := _Nan;
// add a distinct terminal char:
aLine := aLine + #0;
reader := #aLine[1];
while(true) do begin
if reader^= #0 then
exit;
// blank: test the identifier
if reader^ in [#9, ' '] then
begin
if AccumulateTemp then
begin
if not TryStrToFloat(identifier, result) then
result := _Nan;
AccumulateTemp := false;
exit;
end;
if identifier = 'Temp' then
AccumulateTemp := true;
identifier := '';
end else
// accumulate
identifier := identifier + reader^;
Inc(reader);
end;
end;
begin
DecimalSeparator := '.';
writeln( format('%.7f', [getTemperature(line1)]) );
writeln( format('%.7f', [getTemperature(line2)]) );
writeln( format('%.7f', [getTemperature(line3)]) );
writeln( format('%.7f', [getTemperature(line4)]) );
writeln( format('%.7f', [getTemperature(line5)]) );
readln;
end.
which outputs
22.2000000
22.0000000
55.1234560
Nan
Nan

Resources