Hi!
I'm building a Tree that reads words in from a text file and adds it to the tree in sibling or child branches. Everything is being scanned in ok, but something is wrong with Addaword. It seems to not be getting past the first word of the array, and just winds up repeating it. Does anyone know how I would increment it correctly?
*added more code to clarify*
Code:
class Node
{
private:
Node *sibling;
Node *child;
int occurs;
int letters;
public:
AddAWord(char word[], int i);
Search(char request[]);
Node(char l) {occurs=0; sibling=0; child=0; letters=l;}
};
void main(void)
{
Node *root=new Node('-');
Node* new_node;
char* word[80];
int len;
FILE*input;
char* request[10];
char str[80];
int i = 0, k=0, l=0;
ifstream b_file("abook.txt", ios::in);
b_file>>str;
for(int j=0; j < strlen(str); j++)
{
str[j] = tolower((int)str[j]);
}
len = strlen(str);
word[i] = new (char[len+1]);
strcpy(word[i++], str);
root->AddAWord(*word, l);
while(b_file.eof()==0 || i<80)
{
b_file>>str;
for(int j=0; j < strlen(str); j++)
{
str[j] = tolower((int)str[j]);
}
len = strlen(str);
word[i] = new (char[len+1]);
strcpy(word[i++], str);
new_node->AddAWord(word[i], l);
}
cout<<"What word would you like to search for: "<<endl;
cin>>str;
len = strlen(str);
request[k] = new (char[len+1]);
strcpy(request[k++], str);
}
{
cout<<word;
if(letters!=word[0])
{
if(sibling==0)
{
sibling=new Node(*word);
i++;
sibling->AddAWord(word, i);
}
else
{
sibling->AddAWord(word, i++);
}
}
else if(word[i] != '\0')
{
if(child == 0)
{
child=new Node(*word);
printf("%c", word[i]);
i++;
child->AddAWord(word, i);
}
else
{
child->AddAWord(word, i++);
}
}
}