1

This is, as you could tell, a programming assignment for a class. It's far over due and I'm not getting any points however there is a test coming up soon and I'd rather like to know how to use the specific functions in ADA. The program is different now, when I run it with test put statements in the initial procedure, GetStudent, it will output them fine. However now it goes to the bottom, line 96, and gets an end error

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
USE Ada.Integer_Text_IO;
WITH Ada.Float_Text_IO;
USE Ada.Float_Text_IO;

procedure StudentFileLab is
------------------------------------------------------------------------
--| This program stores records in an array, reading them in from a file
--| and writing them out.
------------------------------------------------------------------------

   subtype NameType is String(1..30);
   subtype IDType is natural range 0..9999;
   subtype GPAType is float range 0.0..4.0;

   type StudentRecord is record
      ID          : IDType;
      GPA         : GPAType;
      Name        : NameType := (others => ' ');
   end record;


   subtype StudentIndex is integer range 1..100;
   TYPE StudentArrayType IS ARRAY (StudentIndex) OF StudentRecord;


   -- Specification of input procedure.  You are to write the body.
   PROCEDURE GetStudent (File: IN File_Type; Student : OUT StudentRecord) is

      Length    : Integer;

   BEGIN
      For I In 0..Length Loop
         Get(File, Student.ID);
         For J In 0..Length Loop
            Get(File, Student.GPA);
            For K In 0..Length Loop
               Get_Line (File, Student.Name, Length);
         End Loop;
      END LOOP;
   End LOOP;
  END GetStudent;


   PROCEDURE PutStudent (Student : IN StudentRecord) IS

   BEGIN

      FOR Studentlist IN StudentIndex LOOP
         Put (Student.ID,
              width => 0);
         Put (Item => " ");
         Put (Student.GPA,
            Exp => 0,
            Fore=> 0,
            Aft => 0);
         Put (Item => ", ");
         Put (Student.Name);
         New_Line;
      END LOOP;

   END PutStudent;

   StudentList : StudentArrayType;  -- our array of students

   CurrentIndex : Natural := 0;  -- the index to the current array item
   CurrentStudent : StudentRecord; -- the current student

   Filename : String(1..30);  -- name of the data file
   Flength : Integer;    -- length of the name of the data file
   Infile : File_Type;

begin -- StudentLab
   Put_Line(Item=>"Welcome to the Student Information Program.");
   Put_Line(Item=>"Please enter the student filename: ");
   Get_Line(Item=> Filename, Last => Flength);
   Open(File => Infile, Mode => In_File, Name => Filename(1..Flength));

   loop
      -- Get the next student
      GetStudent(File=> infile, Student => CurrentStudent);
      exit when CurrentStudent.Id = 0;
      CurrentIndex := CurrentIndex + 1;
      StudentList(CurrentIndex) := CurrentStudent;
   END LOOP;
   close(file=>infile);  -- close the data file after all data is read.
   -- Output the header for the nicely formatted output.


   FOR Index IN 1..CurrentIndex loop
      PutStudent(Student => StudentList(Index));
   end loop;


end StudentFileLab;

The Program is supposed to read from a file that looks like this.

1435 3.75 Jane Smith
2233 2.94 Robert Robertson
9634 3.86 Jennie Diver
4325 3.42 Matt Pratt
0

So, Line 96 is literally the end loop line.

   FOR Index IN 1..CurrentIndex loop
   PutStudent(Student => StudentList(Index));
  -----> end loop;

I may be wrong but I feel like my main problem is now with the body of PutStudent seen here:

FOR Studentlist IN StudentIndex LOOP
             Put (Student.ID,
                  width => 0);
             Put (Item => " ");
             Put (Student.GPA,
                Exp => 0,
                Fore=> 0,
                Aft => 0);
             Put (Item => ", ");
             Put (Student.Name);
             New_Line;
          END LOOP;

I feel like it's the for line but I can't tell how to fix it.

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
C0C0B
  • 11
  • 2
  • 4
  • Do you know at what line the error is emitted? Please point it out in your code as well, so readers don't have to count. – Jongware Apr 27 '14 at 22:04
  • it seems like in your first loop, you want to loop from 1 until currentindex that is 0 in beginning. could this be a problem? i think you want to loop from 0 until length instead? – Sara S Apr 27 '14 at 22:06
  • yeah im not going to read through any of that. You have an array that is out of bounds. fix it and your problem is solved. please do not post duplicates and google your questions before posting next time. – Ramulis Apr 27 '14 at 22:07

1 Answers1

5

You aren’t getting End_Error at line 96 of your program, but in the runtime library.

When I run your program, I get

raised ADA.IO_EXCEPTIONS.END_ERROR : a-tigeli.adb:96

which is in fact in Ada.Text_IO.Get_Line.

When I compile your program with all warnings on, I get

studentfilelab.adb:35:19: warning: "Length" may be referenced before it has a value

and, looking at the code, this is

PROCEDURE GetStudent (File: IN File_Type; Student : OUT StudentRecord) is

   Length    : Integer;

BEGIN
   For I In 0..Length Loop              <<<<<<<< line 35
      Get(File, Student.ID);
      For J In 0..Length Loop
         Get(File, Student.GPA);
         For K In 0..Length Loop
            Get_Line (File, Student.Name, Length);
         End Loop;
      END LOOP;
   End LOOP;
END GetStudent;

So, firstly, you would need to set a value for Length; but, more importantly, this procedure is supposed (judging by its name and the context) to read the data for one student, so what are you doing looping in the first place?

GetStudent needs work even after this (you mustn’t try to read anything after the 0 that ends the input data) and I think there are more problems, but that should do to be going on with.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62