Hi, I'm doing an assignment, where we need to use a class Domain and correctly output the URL address.
The given class Domain is like this:

class Domain
{
  public:
    Domain(string lbl) : m_label(lbl) {}
    string label() const { return m_label; }
    const vector<Domain*>& subdomains() const { return m_subdomains; }
    void add(Domain* d) { m_subdomains.push_back(d); }
    ~Domain();
  private:
    string m_label;
    vector<Domain*> m_subdomains;
};
void listAllAuxiliary(string path, const Domain* d)
{
   [B] You will write this code.[/B]
}

void listAll(const Domain* d)
{
    listAllAuxiliary(d->label(), d);
}

int main()
{
    Domain* d1 = new Domain("google");
    d1->add(new Domain("map"));
    d1->add(new Domain("image"));
    d1->add(new Domain("orkut"));
    Domain* d2 = new Domain("microsoft");
    d2->add(new Domain("windows"));
    d2->add(new Domain("msn"));
    Domain* d3 = new Domain("com");
    d3->add(d1);
    d3->add(d2);
    Domain* root = new Domain("");
    root->add(d3);
    listAll(root);
}

The output should look like this:
map.google.com
image.google.com
orkut.google.com
windows.microsoft.com
msn.microsoft.com

So, what I have so far is this, it compiles, but the output is incorrect:

void listAllAuxiliary(string path, const Domain* d)
{	
	path += d->label();
	if (!path.empty())
	{
		cout << path << endl;
		path += '.';
	}
	if ( !d->subdomains().empty())
	{
		const vector<Domain*> sd = d->subdomains();
		for (size_t k = 0; k != sd.size(); k++)
			listAllAuxiliary(path, sd[k]);
	}  
}

The output I'm getting is:
com
com.google
com.google.map
com.google.image
com.google.orkut
com.microsoft
com.microsoft.windows
com.microsoft.msn
Press any key to continue . . .

So, I'm not sure how to correct my code. I know it must be the ORDER in which I'm concatenating the label() to the path and COUTing it in the wrong order too. Any suggestions?

Member Avatar for r.stiltskin

Well, you already know that you're concatenating in the wrong order so you don't need any suggestions regarding that (just fix it), so here are a few other things to think about:

Your code has to handle the case in which the path argument passed to listAllAuxiliary is empty differently from when it is not empty.
According to the sample output, you are supposed to print ONLY for subdomains that have no subdomains themselves. So why are you printing whenever the path argument is not empty?

Thanks for your reply,
I actually got it working before you replied. I changed the way "path" is concatenated with the label() and added some conditions. So I believe it now works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.