0 arrays records ada procedures text-files
正如您所知,这是一个类的编程任务.它已经过时了,我没有得到任何分数,但很快就会有测试,我想知道如何使用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;
Run Code Online (Sandbox Code Playgroud)
该程序应该从一个看起来像这样的文件中读取.
1435 3.75 Jane Smith
2233 2.94 Robert Robertson
9634 3.86 Jennie Diver
4325 3.42 Matt Pratt
0
Run Code Online (Sandbox Code Playgroud)
因此,第96行实际上就是结束循环线.
FOR Index IN 1..CurrentIndex loop
PutStudent(Student => StudentList(Index));
-----> end loop;
Run Code Online (Sandbox Code Playgroud)
我可能错了,但我觉得我的主要问题是现在看到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;
Run Code Online (Sandbox Code Playgroud)
我觉得这是行,但我不知道如何解决它.
您没有进入End_Error程序的第96行,而是进入运行时库.
当我运行你的程序时,我明白了
raised ADA.IO_EXCEPTIONS.END_ERROR : a-tigeli.adb:96
Run Code Online (Sandbox Code Playgroud)
实际上是在Ada.Text_IO.Get_Line.
当我编译所有警告的程序时,我得到了
studentfilelab.adb:35:19: warning: "Length" may be referenced before it has a value
Run Code Online (Sandbox Code Playgroud)
并且,看看代码,这是
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;
Run Code Online (Sandbox Code Playgroud)
所以,首先,你需要设置一个值Length; 但是,更重要的是,这个程序应该(通过它的名称和上下文来判断)来读取一个学生的数据,那么你在做什么呢?
GetStudent即使在此之后也需要工作(在0结束输入数据之后你不得尝试阅读任何内容)并且我认为还有更多问题,但这应该继续下去.