I am trying to read from a text file, slice each line into pieces separated by spaces, put into circular single linked list, perform insert/delete operations and print results. The code compiles but upon running give me "raised GNAT.STRING_SPLIT.INDEX_ERROR : g-arrspl.adb:335 instantiated at g-strspl.ads:39".
I looked at the documentation for g-arrspl.adb line 335 which reads:
elsif Index > S.D.N_Slice then raise Index_Error;
From this I am guessing that I have somehow gone over an index limit somewhere but I don't know how or where.
This is my code so far
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Float_Text_IO;
with GNAT.String_Split;
use GNAT.String_Split;
procedure Main is
package IIO is new Ada.Text_IO.Integer_IO(Integer);
use IIO;
type DepartmentType is(Sales, Crew, IT, Media, Accounting, noDept);
type TitleType is(manager, sales_person, designer, programmer, software_engineer, spokes_person, pilot, copilot, scientist, mission_specialist, notitle);
type NameType is(Bob, Mary, Sally, David, Marty, Vallerie, Sam, Joe, noName);
type CodeType is(IL, IR, DL, DR, PA, PD, DD);
type fileContent is record
code: CodeType;
dept: DepartmentType;
name: NameType;
title: TitleType;
id: Integer := 0;
payrate: Float := 0.00;
end record;
type empNode;
type empPt is access empNode;
type empNode is record
deptName: DepartmentType := noDept;
empName: NameType := noName;
title: TitleType := notitle;
id: Integer := 0;
payrate: Float := 0.00;
next: empPt := null;
end record;
type departmentNode;
type deptPt is access departmentNode;
type departmentNode is record
deptName: DepartmentType;
empName: NameType;
num: Integer := 0;
next: empPt := null;
end record;
fileOut: File_Type;
fileLine: fileContent;
limit: Positive;
package FloatIO is new Ada.Text_IO.Float_IO(Float);
use FloatIO;
package DeptTypeIO is new Ada.Text_IO.Enumeration_IO(DepartmentType);
use DeptTypeIO;
package TitleTypeIO is new Ada.Text_IO.Enumeration_IO(TitleType);
use TitleTypeIO;
package NameTypeIO is new Ada.Text_IO.Enumeration_IO(NameType);
use NameTypeIO;
package CodeTypeIO is new Ada.Text_IO.Enumeration_IO(CodeType);
use CodeTypeIO;
Department: array(DepartmentType) of departmentNode;
inFile: FILE_TYPE;
lineSection: Slice_Set;
procedure inLeftNode(deptNameIn: DepartmentType; empNameIn: NameType; titleIn: TitleType; IDin: Integer; payRateIn: float) is
input: empPt;
begin
input := new empNode'(deptNameIn, empNameIn, titleIn, IDin, payRateIn, null);
if(Department(deptNameIn).next = null) then --empty list
input.next := input;
Department(deptNameIn).next := input;
else --populated list
input.next := Department(deptNameIn).next;
Department(deptNameIn).next := input;
end if;
Department(deptNameIn).num := Department(deptNameIn).num + 1; --number in list
end inLeftNode;
procedure inRightNode(deptNameIn: DepartmentType; empNameIn: NameType; titleIn: TitleType; IDin: Integer; payRateIn: float) is
last: empPt;
first: empPt;
input: empPt;
begin
input := new empNode'(deptNameIn, empNameIn, titleIn, IDin, payRateIn, null);
if(Department(deptNameIn).next = null) then
input.next := input;
Department(deptNameIn).next := input;
else
last := Department(deptNameIn).next;
first := last.next;
input.next := Department(deptNameIn).next;
Department(deptNameIn).next := input;
end if;
Department(deptNameIn).num := Department(deptNameIn).num + 1;
Department(deptNameIn).next := input;
end inRightNode;
procedure deleteLeftNode(deptNameIn: DepartmentType) is
p: empPt;
begin
if(Department(deptNameIn).next = null) then
put_line("Underflow");
else
p := Department(deptNameIn).next;
Department(deptNameIn).next := p.next;
if p = Department(deptNameIn).next then
Department(deptNameIn).next := null;
end if;
end if;
end deleteLeftNode;
procedure deleteRightNode(deptNameIn: DepartmentType) is
Dp, prev, p: empPt;
begin
if(Department(deptNameIn).next = null) then
put_line("Underflow");
else
p := Department(deptNameIn).next;
while p.next /= Department(deptNameIn).next loop
p := p.next;
end loop;
Dp := p.next;
prev := p;
Department(deptNameIn).next := p;
p.next := Department(deptNameIn).next;
if Dp = Department(deptNameIn).next then
Department(deptNameIn).next := null;
end if;
end if;
end deleteRightNode;
procedure deleteDepartment(deptNameIn: DepartmentType) is
begin
null;
end deleteDepartment;
procedure printAll is
begin
null;
end printAll;
procedure printDept(deptNameIn: DepartmentType) is
pt: empPt;
begin
if(Department(deptNameIn).next = null) then
put_line("Department is empty");
put_line(fileOut, "Department is empty");
else
pt := Department(deptNameIn).next;
for emp in 1 .. 5 loop
put(pt.empName);
put(" ");
put(fileOut, pt.empName);
put(fileOut, " ");
pt := pt.next;
end loop;
end if;
end printDept;
begin
Open(inFile, In_File, "Cinput.txt");
skip_line(inFile);
while not End_Of_File (inFile) loop
declare
begin
Create(fileOut, Out_File, "output.txt");
Create(lineSection, get_line(inFile), " ", Multiple);
CodeTypeIO.Get(slice(lineSection, 1), fileLine.code, limit);
DeptTypeIO.Get(slice(lineSection, 2), fileLine.dept, limit);
NameTypeIO.Get(slice(lineSection, 3), fileLine.name, limit);
TitleTypeIO.Get(slice(lineSection, 4), fileLine.title, limit);
IIO.Get(slice(lineSection, 5), fileLine.id, limit);
FloatIO.Get(slice(lineSection, 6), fileLine.payrate, limit);
case fileLine.code is
when IL =>
inLeftNode(fileLine.dept, fileLine.name, fileLine.title, fileLine.id, fileLine.payrate);
when IR =>
inRightNode(fileLine.dept, fileLine.name, fileLine.title, fileLine.id, fileLine.payrate);
when DL =>
deleteLeftNode(fileLine.dept);
when DR =>
deleteRightNode(fileLine.dept);
when PA =>
printAll;
when PD =>
printDept(fileLine.dept);
when DD =>
deleteDepartment(fileLine.dept);
when others =>
null;
end case;
end;
end loop;
end Main;
I've tried changing the numerical values in the printDept for loop thinking it might be going over there but I get the same result.