Lazarus readln不读取变量 [英] Lazarus readln doesn't read the variable
问题描述
我想寻求帮助,因为我无法在过程中的以下代码中识别出我做错了什么,它不会读取记录的第一个单元,但之后会读取uj.nev.它只是第一次错过它,这是我无法理解的,不仅在学校的计算机上,而且也是我的.如果您能向我解释我做错了什么,我将不胜感激.提前致谢! (该程序应该读取记录nev,varos,fizetes,并将它们按升序排序,然后将其写到文本文件中)
I would like to ask for some help as i'm not able to identify what i did wrong in the following code as in the procedure , it doesn't read the first unit of the record , but after that it reads the uj.nev too. It only misses it for the first time which is what i can't understand ,and not only on the school computer but mine too. I would appreciate it if you could explain to me what i did wrong. Thanks in advance! (the program supposed to read records nev , varos , fizetes , and sort them into ascending order, and then write it out into a text file)
program adatbazis;
uses crt,Sysutils;
const
C_FNAME = 'adatbazis.txt';
var
a,n,j,l:integer;
tfout: Textfile;
type
Tember=record
nev:string;
varos:string;
fizetes:longint;
end;
procedure beiras(var uj:Tember);
begin
writeln('nev',j,':');
readln(uj.nev);
writeln('varos',j,':');
readln(uj.varos);
writeln('fizetes',j,':');
readln(uj.fizetes);
end;
var
i,k:integer;
seged:Tember;
tomb: array[1..20] of Tember;
begin
write('n :');
read(n);
for j:= 1 to n do begin
beiras(tomb[j]);
end;
writeln('Mi szerint legyen rendezve?');
repeat
writeln(' Nev:1 , Fizetes:2 , varos:3');
readln(l);
until l<> 1 or 2 or 3;
if l=1 then begin
For j:= 1 to n-1 do begin
for k:= 2 to n do begin
if tomb[j].nev>tomb[k].nev then
begin
seged:=tomb[j];
tomb[j]:=tomb[k];
tomb[k]:=seged;
end;
end;
end;
end;
if l=2 then begin
For j:= 1 to n-1 do begin
for k:= 2 to n do begin
if tomb[j].fizetes>tomb[k].fizetes then
begin
seged:=tomb[j];
tomb[j]:=tomb[k];
tomb[k]:=seged;
end;
end;
end;
end;
if l=3 then begin
For j:= 1 to n-1 do begin
for k:= 2 to n do begin
if tomb[j].varos>tomb[k].varos then
begin
seged:=tomb[j];
tomb[j]:=tomb[k];
tomb[k]:=seged;
end;
end;
end;
end;
AssignFile(tfout, C_FNAME);
rewrite(tfout);
for i:=1 to n do begin
writeln(tfout,tomb[i].nev,' ',tomb[i].varos,' ',tomb[i].fizetes);
end;
CloseFile(tfout);
clrscr;
writeln('done');
readln;
end.
推荐答案
我注意到,与standard input
一起使用时,FPC
Read()
和ReadLn
函数之间的本质区别还没有得到很好的记录(在可以阅读Delphi文档).因此,您的问题的答案如下.
I noticed that the essential difference between the FPC
Read()
and ReadLn
functions when used with standard input
is not very well documented (in the Delphi documentation it can be read). Therefore, the answer to your question is the following.
根据FPC
文档CRLF
,CR
和LF
,所有字符均被识别为End-Of-Line (EOL)
字符,因此在下面的EOL
中代表所有这些字符.
According to the FPC
documentation, CRLF
, CR
and LF
, all are recognized as End-Of-Line (EOL)
characters, so in the following EOL
stands for any of those.
当调用Read(s)
时,其中s: string;
会等待用户输入,直到将EOL
添加到输入缓冲区.直到(但不包括)EOL
的输入字符都从缓冲区中删除,并传递给s
. EOL
留在输入缓冲区中.
When calling Read(s)
where s: string;
the program waits for user input until an EOL
is added to the input buffer. The input characters up until (but excluding) the EOL
are removed from the buffer and passed to s
. The EOL
is left in the input buffer.
对Read(s)
的下一次调用将立即识别输入缓冲区中的EOL
,而不会等待用户输入. EOL
继续保留在缓冲区中,因此以后的Read(s)
将不再等待用户输入.
Next call to Read(s)
will immediately recognize the EOL
in the input buffer, and will not wait for user input. The EOL
continues to remain in the buffer, and therefore no subsequent Read(s)
will wait for user input.
在Read(s)
之后对ReadLn(s)
的第一次调用也将立即检测到缓冲区中的EOL
,而不必等待用户输入.
The first call to ReadLn(s)
after a Read(s)
will also immediately detect the EOL
in the buffer and not wait for user input.
调用ReadLn(s)
时,程序将等待用户输入(如上例所示),直到将EOL
添加到输入缓冲区中为止.直到(但不包括)EOL
的输入字符都从缓冲区中删除,并传递给s
. 然后将EOL
从缓冲区中删除并丢弃.
When calling ReadLn(s)
the program waits (as in previous example) for user input until an EOL
is added to the input buffer. The input characters up until (but excluding) the EOL
are removed from the buffer and passed to s
. Then the EOL
is removed from the buffer and thrown away.
对ReadLn(s)
的下一次呼叫将再次停止并等待用户输入.
Next call to ReadLn(s)
will again stop and wait for user input.
在您的代码中,您将在执行开始时就调用Read(n)
.然后,在beiras()
中,您要调用ReadLn()
,它会在缓冲区中检测到EOL
,因此会立即返回,但还会从缓冲区中删除EOL
.现在,随后对ReadLn()
的调用将等待用户输入.
In your code you are calling Read(n)
in the very beginning of the execution. Then, in beiras()
you are calling ReadLn()
which detects the EOL
in the buffer and therefore returns immediately, but also removes the EOL
from the buffer. Subsequent calls to ReadLn()
will now wait for user input.
治愈是为了在程序开始时将Read()n
更改为ReadLn(n)
.
The cure is iow to change the Read()n
to a ReadLn(n)
in the beginning of your program.
这篇关于Lazarus readln不读取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!