Runtime error when dealing with txt files 
Author Message
 Runtime error when dealing with txt files

Hello,

I have here a program which takes data from some students, calculates
various things, and displays the result. When I try to run this program
I have a runtime error no. 1. I had to post all the file, because I
have no idea where the problem might be, in which procedure, in which
call, so please do not be very harsh with me because of this.

I am trying to use an input file text to get some data from hw1.inp
which already exists, and an output file (called hw2.res)to write to
some other data (based on calculations, etc).

Here is the program, hopefully is not too long to be displayed:

program prog1(indata, outdata);

const studnum = 50; (* maximum number of students in the class *)

type
  studindex = 1..studnum;
  idrange = 1111..9999;
  student = record
      weight1 : real;
      weight2 : real;
      weight3 : real;
      score1 : integer;
      score2 : integer;
      score3 : integer;
      id : idrange;
      avg : real;
      grade : char;
       end;
  studentarray = array[studindex] of student;
        (* array of max 50 students *)
var
  allstud : studentarray;
        i, count : integer
        maxaverage: real; (* calculated maximum average *)
        minaverage: real; (* calculated minimum average *)
        totalavg: real;   (* variable for total average of averages *)
        indata, outdata : text; (* the input/output text files *)

procedure readdata(var stud: student);
(* reads the students data *)
begin
      with stud do begin
    while not eof(indata) do
     begin
      while not eoln(indata) do
       begin
         read(indata, weight1, weight2, weight3);
         read(indata, score1, score2, score3);
         read(indata, id);
       end;
    end;
  end;
end;

procedure findaverage(var stud: student);
(* calculates the average for one student *)
begin
    with stud do
         avg := (weight1*score1)+(weight2*score2)+(weight3*score3);
end;

procedure findmaxavg(studs:studentarray;
(* finds the maximum of the calculated averages from all the students *)
                     var maxaverage: real;
                     var id: idrange);
var i: integer;
 begin
        maxaverage:= studs[1].avg;
        id:= studs[1].id;
        for i:= 1 to studnum do
        if studs[i].avg > maxaverage then
           maxaverage:= studs[i].avg;
        id:= studs[i].id;
  end;

 procedure findminavg(studs: studentarray;
 (* finds the minimum of the calculated averages from all the students
*)
                      var minaverage: real;
                      var id: idrange);
 var i: integer;
 begin
        minaverage:= studs[1].avg;
        id:= studs[1].id;
        for i:= 1 to studnum do
        if studs[i].avg < minaverage then
           minaverage:= studs[i].avg;
        id:= studs[i].id;
 end;

procedure avgofavg(studs: studentarray;
                   var totalavg: real);
(* calculates the average of all the averages for the students*)
var i: integer;
    sum: real;
begin
        sum:= 0;
          for i:= 1 to studnum do begin
              sum:= (sum + studs[i].avg)
              end;
              totalavg:= sum/studnum;
end;

begin (* main program *)
assign(Indata, 'hw1.inp');
reset(Indata);
assign(Outdata,'hw2.res');
rewrite(Outdata);
    while not eof(indata) do
     begin
      while not eoln(indata) do
       begin
         writeln(Outdata,'Please input data for student number ', i);
         readdata(allstud[i]);
         findaverage(allstud[i]);
         end;
     end;
     (* calculates the maximum average, the minimum average and
        the average of averages from the three students averages  *)
         findmaxavg(allstud, maxaverage, allstud[i].id);
         findminavg(allstud, minaverage, allstud[i].id);
         avgofavg(allstud, totalavg);
       writeln(outdata,'ID', 'Average':12);
       writeln(outdata, '*****************************');
       while not eof(outdata) do
        begin
         while not eoln(outdata) do
          begin
           write(outdata, allstud[i].id, allstud[i].avg:9:1);
           writeln(outdata);
          end;
       writeln(outdata,'*****************************');
       write(outdata,'The highest average is: ', maxaverage:2:1, ' for
student ');
       writeln(outdata,allstud[i].id:4);
       write(outdata,'The lowest average is: ', minaverage:2:1, ' for
student ');
       writeln(outdata,allstud[i].id:4);
       write(outdata,'The average of averages is: ', totalavg:2:1);
        end;
     end.

Can anybody please help me figure it out? It must be somewhere at
reading from the hw1.inp or during writing to the hw2.res file, but
it's just so unclear to me.
What does this error message mean?
Thanks in advance,
D.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files

Quote:

>Hello,

>I have here a program which takes data from some students, calculates
>various things, and displays the result. When I try to run this program
>I have a runtime error no. 1.

Error 1 is 'Out of memory'.

Quote:
>I am trying to use an input file text to get some data from hw1.inp
>which already exists, and an output file (called hw2.res)to write to
>some other data (based on calculations, etc).
>procedure readdata(var stud: student);
>(* reads the students data *)
>begin
>      with stud do begin
>    while not eof(indata) do
>     begin
>      while not eoln(indata) do
>       begin
>         read(indata, weight1, weight2, weight3);
>         read(indata, score1, score2, score3);
>         read(indata, id);
>       end;
>    end;
>  end;
>end;

Since indata is a textfile you do not need the eoln, just use readln.

For debugging put in some write variable statements followed by
readln; to see if you are doing what you intended.

The array index needs to be advanced in this procedure.

I did not check for other errors.



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files


Quote:
> Hello,

> I have here a program which takes data from some students,
calculates
> various things, and displays the result. When I try to run this
program
> I have a runtime error no. 1.

As the Programmer's Reference has no doubt told you, runtime error 1
is "Invalid Function Number" - you made a call to a nonexistent DOS
function. That's an odd one, a plain Pascal program with range
checking turned on shouldn't throw that one up. But read on...

Quote:
> I had to post all the file, because I
> have no idea where the problem might be, in which procedure, in
which
> call, so please do not be very harsh with me because of this.

Did it not give a meaningful error address? In the IDE, you can select
Search, Find Error and then give the error address. If it's not
corrupt, the IDE will find it.

Quote:
>...
> program prog1(indata, outdata);

This is not a normal sort of a thing to do in TurboPascal or Borland
Pascal. The program heading is only for decoration. Put indata and
outdata in a Var statement instead... ah, you did that. If you're not
using TurboPascal or Borland Pascal you might be better off asking in
another newsgroup, or at least stating what your compiler is since the
error number would be misleading.

Quote:
>...
> var
>   allstud : studentarray;
>         i, count : integer
>         maxaverage: real; (* calculated maximum average *)

Now, this leads me to suspect that what you have posted is not the
code which is giving the error. This will not compile without a ';'
after 'integer' so maybe this is the wrong haystack to be looking for
this needle in.

Quote:
>         minaverage: real; (* calculated minimum average *)
>         totalavg: real;   (* variable for total average of averages
*)
>         indata, outdata : text; (* the input/output text files *)

> procedure readdata(var stud: student);
> (* reads the students data *)
> begin
>       with stud do begin
>     while not eof(indata) do
>      begin
>       while not eoln(indata) do

Why these whiles? An if I could understand, but you are firstly going
to repeatedly read stud until you reach the end of the current line,
then you are going to keep reading until you reach the end of the
file... except that you don't do a Readln so you're not going to get
too far towards the end of the file.

And don't use 'with' unless you have to (ie unless the teacher
insists). It makes the program less readable because it messes up the
scopes. This program is simple enough that a change would have to be
fairly bizarre to show up the problem, but that is not always true.
Allow a bizarre change... Suppose a few months later you add a field
"indata" of type Text to the student type. Now ReadData would suddenly
be broken although you have not made any changes to it, because when
you wrote "with stud do begin while not eof(indata) do" it cannot be
clear whether you meant stud.indata or some other indata. If you have
to say stud.indata then it's immediately obvious that you don't mean
the global variable, and vice versa.

Quote:
>...
> begin (* main program *)
> assign(Indata, 'hw1.inp');
> reset(Indata);
> assign(Outdata,'hw2.res');
> rewrite(Outdata);
>     while not eof(indata) do
>      begin
>       while not eoln(indata) do
>        begin

More whiles. The while not eof is OK, but the while not eoln is wrong.

Quote:
>          writeln(Outdata,'Please input data for student number ',
i);
>          readdata(allstud[i]);

Now... what is the value of 'i' here? And why do you not have range
checking turned on?

Please turn on all the automatic error checking you can find, until
you have your program debugged. i is undefined here, but a likely
value is 0 (depending on your compiler version). allstud[0] does not
exist since its indices go from 1 to studnum. So you read into a
nonexistent variable and{*filter*}up your system. If you had range
checking on, you would be told that allstud[i] was illegal before you
get into readdata, which would help you if you knew what it meant (I'd
guess it's not obvious if you don't know what to look for. But it's
still better than a crash).

Learn how to use the de{*filter*}. Step through your program. Tidy up your
indenting. Eat your greens. Sit up straight.

Cheers

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files


...

Quote:

> >Hello,

> >I have here a program which takes data from some students,
calculates
> >various things, and displays the result. When I try to run this
program
> >I have a runtime error no. 1.

> Error 1 is 'Out of memory'.

*Compiler* error 1 is 'Out of memory', meaning that the compiler
failed due to lack of memory. Here he is getting a run-time error.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files
Well, no matter what I do, I still get the error message. If I remove
the while for the eoln, then in the first lines of the main program, at:

while not eof(indata) do
 begin
  readdata(allstud[i]);

I get a range check error, error no. 201, and I can't get past that.
Guess I will give up...

D.

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 30 Apr 2003 14:27:08 GMT  
 Runtime error when dealing with txt files
hi


Quote:
> Well, no matter what I do, I still get the error message. If I remove
> the while for the eoln, then in the first lines of the main program, at:

> while not eof(indata) do
>  begin
>   readdata(allstud[i]);

> I get a range check error, error no. 201, and I can't get past that.
> Guess I will give up...

Why?

this one is not too hard.

in your mainprogram you haven't initialized i. so when calling

         readdata(allstud[i]);

this coud be allstud[8754]; definetly out of range (Err.201)

Some new Pascal/ Delphi Compilers have switches to autoinitialize values. But I think it is really no good idea to use them. Even if your compiler does, it will initialize i by 0, but your array is ranged 1..50 (or so)

hth
Jochen



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files
Hi


Quote:
> Well, no matter what I do, I still get the error message. If I remove
> the while for the eoln, then in the first lines of the main program, at:

> while not eof(indata) do
>  begin
>   readdata(allstud[i]);

> I get a range check error, error no. 201, and I can't get past that.
> Guess I will give up...

I have gone through your code and done several changes and
added som comments. take a look at it.

I did not compile the code, so there might be some minor errors within.

hope this helps you.
Jochen

--

program prog1; {(indata, outdata) those are for redefining stdIn and stdOut }

const studnum = 50; (* maximum number of students in the class *)
{ -- what about the real number of students in the class.
     the whole program doesn't take care of this.
     I have missused your global var count for this purpose. }

type
  studindex = 1..studnum;
  idrange = 1111..9999;
  student = record
      weight1 : real;
      weight2 : real;
      weight3 : real;
      score1 : integer;
      score2 : integer;
      score3 : integer;
      id : idrange;
      avg : real;
      grade : char;
  end;
  studentarray = array[studindex] of student;
        (* array of max 50 students *)
var
  allstud : studentarray;
  i, count : integer;
  maxaverage: real; (* calculated maximum average *)
  minaverage: real; (* calculated minimum average *)
  totalavg: real;   (* variable for total average of averages *)

  indata, outdata : text; (* the input/output text files *)

  minStudent,
  maxStudent : idRange;

procedure readdata(var stud: student);
{-- funny way to read in . please show how the data has been written to file }

(* reads the students data *)
begin
  with stud do begin
    while not eof(indata) do
     begin
      while not eoln(indata) do
       begin
         read(indata, weight1, weight2, weight3);
         read(indata, score1, score2, score3);
         read(indata, id);
       end;
    end;
  end;
end;

procedure findaverage(var stud: student);
(* calculates the average for one student *)
begin
  with stud do
    avg := (weight1*score1)+(weight2*score2)+(weight3*score3); {--  average ? }
end;

procedure findmaxavg(studs:studentarray; var maxaverage: real; var id: idrange);
(* finds the maximum of the calculated averages from all the students *)
var i: integer;
begin
  maxaverage:= studs[1].avg;
  id:= studs[1].id;
  for i:= 2 to count do
        if studs[i].avg > maxaverage then
           maxaverage:= studs[i].avg;
        id:= studs[i].id;
  end;

procedure findminavg(studs: studentarray; var minaverage: real; var id: idrange);
 (* finds the minimum of the calculated averages from all the students *)
 { -- warning : you are possibly creating really big local datas on the stack because you do not use var studs:...
      this is of course ok, but you should know about this }
 var i: integer;
 begin
   minaverage:= studs[1].avg;
   id:= studs[1].id;
   for i:= 2 { -- Not 1} to Count { -- ??? studnum  } do
     if studs[i].avg < minaverage then
     begin
       minaverage:= studs[i].avg;
       id:= studs[i].id; { -- this must be inside your loop. it wasn't }
     end  
 end;

procedure avgofavg(studs: studentarray; var totalavg: real);
(* calculates the average of all the averages for the students*)
{ -- warning : look above }
var i: integer;
    sum: real;
begin
  sum:= 0;
  for i:= 1 to count do
    sum:= sum + studs[i].avg;
  totalavg:= sum/count;
end;

begin (* main program *)
  assign(Indata, 'hw1.inp');
  reset(Indata);
  assign(Outdata,'hw2.res');
  rewrite(Outdata);
  {-- Errorhandling ??? }

  count := 0;
  while not eof(indata) do
  begin
    count := count + 1;
    {-- ??? writeln(Outdata,'Please input data for student number ', i); }
    readdata(allstud[count]);
    findaverage(allstud[count]);
  end;
  close(indata);

  (* calculates the maximum average, the minimum average and
     the average of averages from the three students averages  *)

  findmaxavg(allstud, maxaverage, MaxStudentID); { -- ??? allstud[i].id }
  findminavg(allstud, minaverage, MinStudentID); { --- ??? allstud[i].id);
  avgofavg(allstud, totalavg);
  writeln(outdata,'ID':3, 'Average':12);
  writeln(outdata, '*****************************');
  { -- ???
   while not eof(outdata) do
        begin
         while not eoln(outdata) do
          begin
           write(outdata, allstud[i].id, allstud[i].avg:9:1);
           writeln(outdata);
          end;

  }
  {-- Try somthing like this : }

  for i:=1 to count do
    writeln(outdata, allstud[i].id:5, allstud[i].avg:9:1);

  writeln(outdata,'*****************************');
  write(outdata,'The highest average is: ', maxaverage:6:1, ' for student ');
  writeln(outdata,maxStudentID:4);
  write(outdata,'The lowest average is: ', minaverage:6:1, ' for student ');
  writeln(outdata,minStudentID:4);
  write(outdata,'The average of averages is: ', totalavg:6:1);
  close(outdata)
end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime error when dealing with txt files
Dear Jochen,

Thanks for all the effort you put in helping me with this work.
You are asking for the real number of students in the class. The
problem is, we don't know the real number of the students, basically in
the hw1.inp file each new line means beginning of a new students. So
right now 3 lines alltogher in my input file, each line having the data
(the 3 weights, 3 scores and the student ID) for one student. If a new
line is being added to the file, then my program should recognise that
I have now 4 students to take care of, and should function correctly.
I guess that's why I will have to use while not eof(indata) and eoln
(indata) and not a for loop.
The count I was going to use in the average of average calculation,
where I determing the real number of students (after I read in the data
from the input file) and divide the total by that count.
By the way, yes, I calculate the weighted average that way
(weight1*score1)+(weight2*score2)+(weight3*score3). The weight is
always smaller than 1 and the score is max 100, so I never get higher
numbers than 100. One average could be ex. 65, that would mean a D for
that student.

Thanks again for your help,
D.

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Urgent -- Saving a Turbo Pascal file into .tXt file

2. Runtime error 005 upon RESET of file passed from Windows

3. A bug in Pascal RESET gives runtime error file isnt open

4. TP7 and Runtime error 006 - Invalid file handle.

5. Logging Runtime Error Codes and location to file before exitting program

6. runtime error 4 : To Many Files Open.

7. Too Many Files Open (runtime error 4)

8. using FOO <toto.txt >tata.txt

9. dealing with ID3tags (in MP3 files)

10. Unit to deal with long file names under dos 7.0

11. Heap Overflow Error - runtime error 203

12. Print TXT file under Delphi

 

 
Powered by phpBB® Forum Software