• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Display Woe In Delphi

For an assignment i am trying to display particular values stored in a record structure (of type ternary tree). The data is read in from a text file. There is no input value, it simply displays 2 select values from the tree.

I am trying to test to make sure it isnt emtpy. If its not, i want to display the values and then move to the next branches and so on until the values are empty.

I am getting lots of errors though which i dont understand. Can anyone help?

procedure findPhrase (t:MelodyTree; n: integer);
begin
 writeln;writeln;writeln;writeln;
 writeln(' Phrases Stored In The Tree Are: ');
  if (t^.title <> nil) and (t^.phrase <> nil) then
write('[', findPhrase(t^.title,n+1) ']  :  ', '"', findPhrase(t^.phrase,n+1) '"');
end;
 
// Record Structure
type MelodyTree = ^TreeNode;
     TreeNode = record
                  upBranch: MelodyTree;
                  downBranch: MelodyTree;
                  sameBranch: MelodyTree;
                  title: string;
                  phrase: string;
                end;

Open in new window

Untitled.jpg
0
Prima12
Asked:
Prima12
  • 3
  • 2
1 Solution
 
ThievingSixCommented:
First off:

t^.title <> nil

Your dereferencing the pointer with "^" so it's not a pointer value anymore so you can't compare it with. I'm not sure exactly how the structure is written to the text file when the values are empty. Maybe you could provide more insight to that.

-

Second when you recursively call

findPhrase(t^.title,n+1)

Your trying to give it a string variable and it wants a MelodyTree type. So depending on how this works you will use FindPhrase(T,N+1). You don't show how it actually finds the record so it's hard to see whats wrong.
0
 
ThievingSixCommented:
This is the best way you could probably check if the tree is invalid.

Since we are reading from a text file for records the address for a string can't be 0(nil). So we can only check to see if the entire tree exists(Assigned) and if the length of the strings are 0(after removing whitespace).
procedure findPhrase (t: MelodyTree);  //Write an inorder traversal using a stack
begin
  //If T is nil or the length of the title or phrase is 0
  If (Not(Assigned(T)) And ((Length(Trim(T^.title)) = 0) Or (Length(Trim(T^.phrase)) = 0)) Then
    begin
    Exit;
  end;
  writeln;
  writeln(' Phrases Stored In The Tree Are: ');
  write('[', t^.title, ']  :  ', '"', t^.phrase, '"');
end;

Open in new window

0
 
Prima12Author Commented:
I still cant get any output though to begin checking if its working. I keep getting those errors that tell me that t^.title is incompatible. I dont understand why this happens, as all my books and notes use integers to navigate and i have no examples of any that use strings.

0
 
Prima12Author Commented:
This gives me no output at all. And i really really do not see why it wont.
procedure findPhrase (t: MelodyTree; n:integer);  //Use inorder traversal
begin
 writeln;
 writeln(' Phrases Stored In The Tree Are: ');
 
If (Not(Assigned(T))) And ((Length(Trim(T^.title)) = 0) Or (Length(Trim(T^.phrase)) = 0)) Then
 begin
    findPhrase(t, n+1);
    write('[', (t^.title), ']  :  ', '"', (t^.phrase), '"');
 end;
end;

Open in new window

0
 
Prima12Author Commented:
Thanks for the suggestion. I have now opened a differnet question regarding the traversal of the tree.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now