Pascal - 错误的数字格式
程序:
program s;
type
info = record
name, surname: string;
min, sek: integer;
end;
type arrays = array[1..50] of info;
var
c, b: text;
A: arrays;
gr_sk, grup_dal: integer;
begin
assign(c, 'info.txt');
reset(c);
read(c, gr_sk);
read(c, grup_dal);
id := 1;
read(c, A[id].name);
read(c, A[id].sek);
close(c);
end.
info.txt 文件:
3
4
yhgf
4
请告诉我哪里出了问题。我猜它说第 19 行的数字格式不正确。
如果我将 min, sek: integer;
更改为 min, sek: string;
那么它就可以工作。据我了解,它读取数字就像字符串一样。怎么可能呢?我以前从未经历过这种情况。
Program:
program s;
type
info = record
name, surname: string;
min, sek: integer;
end;
type arrays = array[1..50] of info;
var
c, b: text;
A: arrays;
gr_sk, grup_dal: integer;
begin
assign(c, 'info.txt');
reset(c);
read(c, gr_sk);
read(c, grup_dal);
id := 1;
read(c, A[id].name);
read(c, A[id].sek);
close(c);
end.
info.txt file:
3
4
yhgf
4
Please, tell me what is wrong with that. It says that it is bad number format for line 19 I guess.
If I change min, sek: integer;
to min, sek: string;
then it works. So as I understand, it reads number like string. How can it be? I have never experienced that before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我认为
您正在尝试将“yhgf”读入整数(gr_sk),因此当读取时,它会抛出错误,因为“yhgf”无法转换为整数。
你应该做什么?
好吧,我认为您可以将其读入字符串,验证它是一个数字,然后将其转换为整数。坦率地说,我不记得帕斯卡的做法。经过谷歌搜索后,发现val过程。
有关字符串函数/过程的一些提示:
This is what i think
You are trying to read 'yhgf' into an integer (gr_sk), so when read reads, it throws an error because 'yhgf' can't be transformed into an integer.
What should you do?
Well, I think that you can read it into a string, validate that it is a number, and then transform it into an integer. Frankly, I don't remember the Pascal way to do it. After googling around, found val procedure.
Some tips on string functions / procedures:
我已经使用旧 Pascal很多年了(现在我使用Delphi),并且我认为您错误地使用了
c
变量。无论如何,在 Pascal 中,您可以声明file
变量并在它们上关联记录类型。例子:
I have many years to use old Pascal (nowadays I use Delphi), and I think you are using your
c
variable wrongly. Anyway, in Pascal you can declarefile
variables and associate a record type on them.Example:
更改
为
无论如何我都会朝那个方向搜索问题;即不读取行分隔符。 (CR 和 LF)
如有疑问,请执行 read(f,) 并将一些读取字符的 ORD() 写入屏幕
Change
to
Anyway I would search the problem in that direction; namely the not reading of the lineseparator. (CR and LF)
When in doubt, do a read(f,) and write the ORD() of a few of the read chars to screen