How can I indent cout output?

Fantomas picture Fantomas · Oct 11, 2009 · Viewed 25.8k times · Source

I'm trying to print binary tree

void print_tree(Node * root,int level )
 {
    if (root!=NULL)  
    {  
        cout<< root->value << endl;
    }
    //...
}

How can I indent output in order to indent each value with level '-' chars.

Answer

Daniel Earwicker picture Daniel Earwicker · Oct 11, 2009

You can construct a string to contain a number of repitions of a character:

std::cout << std::string(level, '-') << root->value << std::endl;