Recent

Author Topic: help for solving my question (Pascal)  (Read 7150 times)

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
help for solving my question (Pascal)
« on: October 01, 2016, 12:05:19 pm »
Hi there, I hope someone can help! I'm new to Pascal programming and I'm
trying to read records from a text file, but exit code=106

Code: [Select]
type
        condition=record
                                no:string[30];
                                time_left:integer;
                             end;
var traffic:array[1..20] of condition;
procedure input_traffic(var count2:integer);
var count:integer;
begin
        count2:=0;
        assign(trafficfile,'C:/trafficcontrol/traffic.txt');
        reset(trafficfile);
        while not eof(trafficfile) do
        begin
                count2:=count2+1;
                with traffic[count2] do
                begin
                        readln(trafficfile,no);
                        readln(trafficfile,landing_time_left);
                end;
        end;
close(trafficfile);
end;
begin
count2:=0;
input_traffic(count2)
end.
7vinbaby

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: help for solving my question (Pascal)
« Reply #1 on: October 01, 2016, 12:11:27 pm »
First... did you lookup what error code 106 means?
http://www.freepascal.org/docs-html/user/userap4.html

Quote
106 Invalid numeric format
Reported when a non-numeric value is read from a text file, and a numeric value was expected.

So how does your txt file look like?
Do you have a number for every string?

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: help for solving my question (Pascal)
« Reply #2 on: October 01, 2016, 12:14:27 pm »
First... did you lookup what error code 106 means?
http://www.freepascal.org/docs-html/user/userap4.html

Quote
106 Invalid numeric format
Reported when a non-numeric value is read from a text file, and a numeric value was expected.

So how does your txt file look like?
Do you have a number for every string?

my txt file looks like that.

abc1234567
5
bcd1234567
10
cde1234567


any problems on that ?
7vinbaby

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: help for solving my question (Pascal)
« Reply #3 on: October 01, 2016, 12:17:05 pm »
my txt file looks like that.

abc1234567
5
bcd1234567
10
cde1234567


any problems on that ?
yes, there is.

Look at your code. You read a string and after that you ALWAYS read a number. But after the last string in your txt you don't have a number anymore. So the last read() which expects a number, it encounters an "end of file".

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: help for solving my question (Pascal)
« Reply #4 on: October 01, 2016, 12:20:09 pm »
my txt file looks like that.

abc1234567
5
bcd1234567
10
cde1234567


any problems on that ?
yes, there is.

Look at your code. You read a string and after that you ALWAYS read a number. But after the last string in your txt you don't have a number anymore. So the last read() which expects a number, it encounters an "end of file".
However after i correct it , there is still run-time error106

new txt file:


abc1234567
5
bcd1234567
10
cde1234567
15

what is my problems ?
« Last Edit: October 01, 2016, 12:23:39 pm by 7vinbaby »
7vinbaby

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: help for solving my question (Pascal)
« Reply #5 on: October 01, 2016, 12:24:40 pm »
That's probably because you have an enter after the last number. In that case there is no "end of file" so there is a g empty) string read again and again without a number.

Delete the extra carriage return after the number and it should be ok.

Otherwise you need to check on something else for end of file. You could put a eof (in string) yourself and after reading the string you could check it and exit the loop.

Or put a eof() check just before reading the number.

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: help for solving my question (Pascal)
« Reply #6 on: October 01, 2016, 12:30:05 pm »
That's probably because you have an enter after the last number. In that case there is no "end of file" so there is a g empty) string read again and again without a number.

Delete the extra carriage return after the number and it should be ok.

Otherwise you need to check on something else for end of file. You could put a eof (in string) yourself and after reading the string you could check it and exit the loop.

Or put a eof() check just before reading the number.

Sorry,i am not quite sure about what you mean.
Do you mean i should correct my code and the txt file is no problem?
7vinbaby

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: help for solving my question (Pascal)
« Reply #7 on: October 01, 2016, 12:38:33 pm »
Sorry,i am not quite sure about what you mean.
Do you mean i should correct my code and the txt file is no problem?
You can do one or the other (or both).

For you code the file likes like this
-------
abc1234567
5
bcd1234567
10
cde1234567
15

---------
Notice the empty line at the end. Your code sees that as another string and not eof(). If you have a good editor... can you put the cursor under the 15? If so... Press backspace one time so you remove that extra line.

But if you are working with different files (or you change it a lot) it's best to check for eof() before reading the number. If it is eof(), skip reading the number.

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: help for solving my question (Pascal)
« Reply #8 on: October 01, 2016, 01:00:31 pm »
Sorry,i am not quite sure about what you mean.
Do you mean i should correct my code and the txt file is no problem?
You can do one or the other (or both).

For you code the file likes like this
-------
abc1234567
5
bcd1234567
10
cde1234567
15

---------
Notice the empty line at the end. Your code sees that as another string and not eof(). If you have a good editor... can you put the cursor under the 15? If so... Press backspace one time so you remove that extra line.

But if you are working with different files (or you change it a lot) it's best to check for eof() before reading the number. If it is eof(), skip reading the number.

ok i know the problem ,thank you !!
7vinbaby

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: help for solving my question (Pascal)
« Reply #9 on: October 01, 2016, 01:11:38 pm »
ok i know the problem ,thank you !!
And you must more checks. Like this:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6.   TCondition = record
  7.     No: string[30];
  8.     TimeLeft: Integer;
  9.   end;
  10.  
  11. procedure InputTraffic(var Store: array of TCondition; out Count: Integer);
  12. var
  13.   TrafficFile: TextFile;
  14.   Line1, Line2: string;
  15.   Time, Err: Integer;
  16. begin
  17.   Count := 0;
  18.   AssignFile(TrafficFile, 'C:\trafficcontrol\traffic.txt');
  19.   Reset(TrafficFile);
  20.   try
  21.     while not EOF(TrafficFile) and (Count <= High(Store)) do
  22.     begin
  23.       Readln(TrafficFile, Line1);
  24.       if Length(Line1) > High(TCondition.No) then
  25.         Break;
  26.       if EOF(TrafficFile) then
  27.         Break;
  28.       Readln(TrafficFile, Line2);
  29.       Val(Line2, Time, Err);
  30.       if Err <> 0 then
  31.         Break;
  32.       Store[Count].No := Line1;
  33.       Store[Count].TimeLeft := Time;
  34.       Inc(Count);
  35.     end;
  36.   finally
  37.     CloseFile(TrafficFile);
  38.   end;
  39. end;
  40.  
  41. var
  42.   Traffic: array [1..20] of TCondition;
  43.   Count: Integer;
  44. begin
  45.   InputTraffic(Traffic, Count);
  46.   Writeln('Count = ', Count);
  47. end.

 

TinyPortal © 2005-2018