2014-04-27 184 views
0

正如你所知,這是一個課程的編程任務。它已經到期了,我沒有得到任何意見,但是很快會有一個測試,我想知道如何使用ADA中的特定功能。現在程序不同了,當我在初始程序GetStudent中使用test put語句運行它時,它將輸出它們很好。但是現在它進入底部線96,並得到最終錯誤爲什麼我會收到Ada錯誤?

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; 

我覺得它的線路,但我不能告訴如何修理它。

+0

你知道在哪條線上發出錯誤嗎?請在您的代碼中指出,以便讀者不必計數。 – usr2564301

+0

它看起來像在你的第一個循環中,你想從1循環到currentindex,在開始時爲0。這可能是一個問題嗎?我想你想循環從0到長度,而不是? –

+0

是的,我不打算通讀任何。 你有一個超出界限的數組。修復它並解決您的問題。請不要在下次發佈之前發佈重複內容和Google問題。 – Ramulis

回答

5

您沒有在程序的第96行獲得End_Error,但在運行時庫中。

當我運行您的程序,我得到

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之後嘗試讀取任何結束輸入數據的內容),我認爲還有更多問題,但應該繼續。

相關問題