Problem with a function argument

I'm trying to parse a math expression into a tree. My question is of what type the "root" argument should be?
I guess this is where my program crushes. Here is the code of that function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void TreeParser(string str ,TreeNode*& root)
{

char  ch;

    
root = new TreeNode();
TreeNode* lastNode = root;



 for(int i=0; i<= str.size(); ++i)
 {
   ch = str[i];
         
   if(ch >= '0' && ch <= '9' )
    {
        if(i!=0 && str[i-1] >= '0' && str[i-1] <= '9'  )
        {
            lastNode->number = lastNode->number*10 + atof(&ch);    
        }
        else
        {
            //TreeNode* tempNode = new TreeNode(atof(&ch));
            if(lastNode->right != NULL)
            {
               if(lastNode->op == '/' && atof(&ch) == 0)
               {
                  cout<<endl<<"Cannot devide by zero - Reset computer Now!!!"<<endl;
                  break;
               }
               lastNode->right = new TreeNode(atof(&ch));
               lastNode = lastNode->right;
            }
            else
            {
                lastNode->left = new TreeNode(atof(&ch));
                lastNode = lastNode->left;
            }
        }
    }
        
    if(str[i] == '*' || str[i] == '/')
    {
         lastNode->left = new TreeNode(lastNode->number);
         lastNode->op = str[i];
         lastNode->number = 0;          
    }
        
    if(str[i] == '+' || str[i] == '-')
    {
        TreeNode* newRoot = new TreeNode(str[i], root, NULL);
        root = newRoot;
    }
    
    
   
 }
}

Im using the node "root" in another function evaluating the tree.
Thank you in advance!
Last edited on
The only thing I see so far is on line 12:
for(int i=0; i<= str.size(); ++i)
The variable i would step 1 character past the end of str which should crash when str[i] is accessed. Try it without the =.
Last edited on
Hm.. you are right, but it seems the problem is not there :(
So root should be of type TreeNode*& ?
i didn't look your code completely but i never used such kind of parameter: pointer to reference.
you have two alternatives:
1. instantiate the TreeNode root object and give it as a pointer argument to your function
2. create it in the function and return it as object: TreeNode TreeParser(string str) { ... return root; }

An advice: learn the basics and differences between instance variable, pointer and reference.
hello all,
I am a beginner in C++. i tried to run the program i copied from this site below but it would execute. i am using C++ version 5.02. Any helps pls.
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}
well.. you'd better post this in another topic
@marko: Pointer references are actually used. They break encalsulation somewhat, but it allows you to change the actual pointer you are passed. E.g. to insert a node in the head of a linked list you can do

void headInsert(Node*& head, T newNodesData)
{
Node* temp = new Node();
temp->data = newNodesData;
temp->next = head;
head = temp;
}

This can only be done by passing head as a pointer reference, otherwise the last line would modify a local copy of head only.
Topic archived. No new replies allowed.