So I'm compiling Ada with the Gnat compiler. I have a queue that is holding a record and one of the records variables is an Unbounded_String called name. That is what I'm testing right now although all the records variables should be accessible outside of the loop. The code I'm having issues with is
with ada.text_io, stacks, queues, ada.Strings.Unbounded, ada.Strings.Unbounded.Text_IO, ada.Integer_Text_IO;
use ada.Text_IO, ada.Strings.Unbounded, ada.Strings.Unbounded.Text_IO, ada.Integer_Text_IO;
procedure eliminate is
type person is record
index: Positive;
age: Positive;
skill: natural;
wins: natural;
loss: natural;
name: Unbounded_String;
end record;
package stkPkg is new stacks(person);
use stkPkg;
output: Stack;
package quePkg is new Queues(person);
use quePkg;
noLossQ : Queue;
count : Positive := 1;
input : Person;
begin
while not end_of_file loop
input.name := get_line;
get(input.skill);
get(input.age);
skip_line;
input.index := count;
count := count + 1;
input.wins := 0;
input.loss := 0;
enqueue(input, noLossQ);
put_line(front(noLossQ).name);
end loop;
put_line(front(noLossQ).name);
end eliminate;
If you run it you'll notice that inside the loop put_line(front(noLossQ).name) works just fine, but once outside the loop it does nothing or prints a blank line. The end of the loop where the put_line worked, the loop ended, and then put_line doesn't work; that code follows.
enqueue(input, noLossQ);
put_line(front(noLossQ).name);
end loop;
put_line(front(noLossQ).name);
Here is my Queue implementation
with Unchecked_Deallocation, ada.Text_IO;
use ada.Text_IO;
package body queues is
function is_Empty (Q: Queue) return Boolean is
begin
return ((Q.front = null) and (Q.back = null));
end is_Empty;
procedure Dequeue (Q : in out Queue) is
begin
if is_Empty(Q) then
raise Queue_Empty;
else
Q.Back := Q.Back.Next;
if Q.Back = null then
Q.Front := null;
end if;
end if;
end Dequeue;
procedure Enqueue (Item: ItemType; Q: in out Queue) is
tmp : QueueNodePointer := new QueueNode'(Item, null);
begin
if is_Full(q) then
raise Queue_Full;
else
if Q.Back = Null then
Q.Back := tmp;
end if;
if Q.Front /= null then
q.front.next := tmp;
end if;
q.Front := tmp;
end if;
end Enqueue;
function Front (Q: Queue) return ItemType is
begin
if is_Empty(Q) then
raise Queue_Empty;
else
return Q.back.data;
end if;
end Front;
end queues;
redirected file I'm using:
Person one
500 50
Person 2
400 50
Person 3
300 50
Person 4
200 50
Person 5
100 50
I can't figure out why it only works in the loop, any help is appreciated. Thanks in advance!!
queues.ads. Also,FrontreturnsQ.back.datawhich is at the very least confusing! - Simon Wright