Pascal - 错误的数字格式

发布于 2024-09-01 16:56:47 字数 613 浏览 4 评论 0原文

程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福丶如此 2024-09-08 16:56:47

这就是我认为

您正在尝试将“yhgf”读入整数(gr_sk),因此当读取时,它会抛出错误,因为“yhgf”无法转换为整数。

你应该做什么?

好吧,我认为您可以将其读入字符串,验证它是一个数字,然后将其转换为整数。坦率地说,我不记得帕斯卡的做法。经过谷歌搜索后,发现val过程

Val 转换整数或实数
所代表的数字
字符串 Source 中的字符和
将其放入 x 中。

有关字符串函数/过程的一些提示:

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.

Val converts the integer or real
number that is represented by the
characters in the string Source and
places it into x.

Some tips on string functions / procedures:

白鸥掠海 2024-09-08 16:56:47

我已经使用 Pascal很多年了(现在我使用Delphi),并且我认为您错误地使用了c变量。无论如何,在 Pascal 中,您可以声明 file 变量并在它们上关联记录类型。

例子:

  type info ...

  var f: file info; { <-- here }
  ...

  begin
     assign(f, 'info.txt');
     reset(f);
     id := 1;
     read(f, A[id]);
     close(f);
  { now A[1] should contain file data }
  ...

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 declare file variables and associate a record type on them.

Example:

  type info ...

  var f: file info; { <-- here }
  ...

  begin
     assign(f, 'info.txt');
     reset(f);
     id := 1;
     read(f, A[id]);
     close(f);
  { now A[1] should contain file data }
  ...
冷月断魂刀 2024-09-08 16:56:47

更改

read(c, A[id].name);

readln(c, A[id].name);

无论如何我都会朝那个方向搜索问题;即不读取行分隔符。 (CR 和 LF)

如有疑问,请执行 read(f,) 并将一些读取字符的 ORD() 写入屏幕

Change

read(c, A[id].name);

to

readln(c, A[id].name);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文