Pascal - bad number format
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 lin开发者_Go百科e 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.
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:
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 }
...
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
精彩评论