Trying to simulate a linux shell.Problems with simulating mkdir : <p>I am trying to make a c++ program to simulate a Linux shell. Nothing to fancy, just a few commands.</p>

<p>I am trying to do it like this:</p>

<p>I made a Class that tries to simulate a file system with Nodes and children.
Every node can have n children and every child has a pointer pointing to its parent.</p>

<p>The Node class looks like this:</p>

<pre><code>class Node
{
public:
    Node();
    Node(bool isFile);
    Node(string name);
    friend class Tree;
    bool isFile;
    vector&lt;Node*&gt; childs;
    string name;
    string text;
    Node *parrent;
    string names = "-";

protected:

private:



};
</code></pre>

<p>I made an addNode method that I want to use to add Nodes to my "Tree". And what I tried to do is have it like this:</p>

<pre><code>void Tree::addNode(string name,bool isFile,string pwd)
{
size_t pos=0;
string delimiter = "\\";
string token;
Node *inserter = new Node();
Node *aux;
inserter-&gt;name=name;
inserter-&gt;isFile=isFile;
aux=&amp;this-&gt;root;
pos = pwd.find(delimiter);
token = pwd.substr(0,pos);
pwd.erase(0,pos + delimiter.length());
if((pos = pwd.find(delimiter)) == string::npos)
{
    inserter-&gt;parrent=aux;
    if(aux-&gt;names.find(name) == string::npos)
    {
        aux-&gt;childs.push_back(inserter);
        aux-&gt;names.append(name);
        aux-&gt;names.append(" ");
    }
    else
    {

        cout &lt;&lt; "The directory already exists" &lt;&lt; endl;
    }
}
else
{
    while((pos = pwd.find(delimiter)) != string::npos)
    {
        token = pwd.substr(0,pos);
        pwd.erase(0,pos + delimiter.length());
        for(int i = 0; i&lt;aux-&gt;childs.size(); i++)
        {
            if(aux-&gt;childs[i]-&gt;name.compare(token))
            {
                aux=aux-&gt;childs[i];
                cout &lt;&lt; token &lt;&lt; endl;
            }
        }

    }
    inserter-&gt;parrent=aux;
    if(aux-&gt;names.find(name) == string::npos)
    {
        aux-&gt;childs.push_back(inserter);
        aux-&gt;names.append(name);
        aux-&gt;names.append(" ");
    }
    else
    {

        cout &lt;&lt; "The directory already exists" &lt;&lt; endl;
    }
}

 }
</code></pre>

<p>So, basically, every time I call this method, I want to make sure that every directory exists, and I change the pointer aux to point at the child address, and so on.</p>

<p>The first if is for making directories in the root, and what's in the while for every other directory.</p>

<p>But, due to my bad understanding of pointers, I can't see where I go wrong.
When I add nodes in the "root" node everything works fine , but when I add nodes in any children things go bad, and I can't quite put my finger on it.</p>

<p>I also have a ls and cd implemented
The ls works fine, but the cd doesn't.I try to do the same thing when checking for the existance of directories, but sometmes, even if a directory doesn't exist, if you type cd "name of directroy that you are in" it just adds to pdw even if that directory doesn't exist.</p>

<p>Here is the cd method</p>

<pre><code>bool Tree::exitsChild(string name,string pwd)
{
size_t pos=0;
string delimiter = "\\";
string token;
Node *aux;
aux=&amp;root;
pos = pwd.find(delimiter);
token = pwd.substr(0,pos);
pwd.erase(0,pos + delimiter.length());
if((pos = pwd.find(delimiter)) == string::npos)
{
    if(aux-&gt;names.find(name)!= string::npos)
    {
        return true;
    }
    else
        return false;
}
else
{
    while((pos = pwd.find(delimiter)) != string::npos)
    {
        token = pwd.substr(0,pos);
        pwd.erase(0,pos + delimiter.length());
        for(int i = 0; i&lt;aux-&gt;childs.size(); i++)
        {
            if(aux-&gt;childs[i]-&gt;name.compare(token))
            {
                aux=aux-&gt;childs[i];
            }
        }
    }
    if(aux-&gt;names.find(name) != string::npos)
        {
            return true;
        }
        else
        {
            return false;
        }
}

}
</code></pre>
