为什么我收到Ada错误?

时间:2014-04-27 22:01:09

标签: arrays text-files ada records procedures

正如您所知,这是一个类的编程任务。它已经过时了,我没有得到任何分数,但是很快就会有测试,而我更愿意知道如何使用ADA中的特定功能。程序现在不同了,当我在初始程序GetStudent中使用test put语句运行它时,它会输出它们。但是现在它转到底部,第96行,并得到一个结束错误

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;

本程序应该从一个看起来像这样的文件中读取。

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

因此,第96行实际上就是结束循环线。

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

我可能错了,但我觉得我的主要问题是现在看到PutStudent的主体:

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;

我觉得它是for line,但我无法告诉如何修复它。

1 个答案:

答案 0 :(得分:5)

您没有在程序的第96行获得End_Error,而是在运行时库中获得。{/ p>

当我运行你的程序时,我得到了

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

实际上是Ada.Text_IO.Get_Line

当我编译所有警告的程序时,我得到了

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

并且,查看代码,这是

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;

首先,您需要为Length设置一个值;但是,更重要的是,这个程序(根据其名称和背景判断)来读取一个学生的数据,那么你首先要做什么循环呢?

GetStudent即使在此之后也需要工作(你不得试图在结束输入数据的0之后阅读任何内容)并且我认为还有更多问题,但这应该是on with。