Hi all,
I want to replace the "string word" within the code below with "const char * word". But when I do that,I get an error message that a const char * can' t be on the left side of "."
Can someone help me with that please?
Code:
#include <iostream>
#include <string>
#include <string.h>
#include <new>
#include <stdlib.h>
#include <exception>
using namespace std;
struct Tnode
{
char word[64+1];
int count;
Tnode* left;
Tnode* right;
};
class Tree
{
public:
Tnode* addNode(Tnode* root, string word);
void printTree(Tnode* root);
void alphabeticPrintTree(Tnode* root);
void bprintTree(Tnode* root);
void freeTree(Tnode* root);
};
Tnode* Tree::addNode(Tnode* root, string word)
{
Tnode* node;
if (!root)
{
node = new Tnode;
size_t copyLen = min(sizeof(node->word)-1, word.siz());
strncpy(node->word, word.c_str(), copyLen);
node->word[copyLen] = '\0';
node->left = 0;
node->right = 0;
node->count = 1;
return (node);
}
int cmp = strcmp(word.c_str(), root->word);
if (cmp < 0)
{
node = addNode(root->left, word);
if(!root->left)
{
root->left = node;
}
}
else if (cmp > 0)
{
node = addNode(root->right, word);
if(!root->right)
{
root->right = node;
}
}
else
{
root->count++;
}
return (node);
}
...
int main(int argc, char* argv[])
{
Tree tree;
Tnode* root = 0;
string word;
try
{
for(int i = 1; i < argc; i++)
{
word = string(argv[i]);
Tnode* node = tree.addNode(root, word);
if (!root)
{
root = node;
}
}
cout << "Tree:" << endl;
tree.printTree(root);
cout << "Alphabetical:" << endl;
tree.alphabeticPrintTree(root);
tree.freeTree(root);
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return (0);
}
Thank you in advance.