title: Keeping count in recursive function C++
body: <p>I have a function that's traversing an AVL tree in level-order. The output format is:</p>
  
  <pre><code>level 0: jim(2)
  level 1: bob(1) joe(1)
  </code></pre>
  
  <p>But when I get to level 4 and up, I want to break it down so it will only show 8 items per line. So the out put would start to look like this:</p>
  
  <pre><code>level 4: item1(1) item2(1) item3(2) item4(2) item5(1) item6(2) item7(1) item8(2)
  level 4: item9(2) item10(2)
  </code></pre>
  
  <p>Right now my code will display all the items, but only on one line. I'm having trouble figuring out how to improve this code so that it is formatted the way I want. How can I implement this?</p>
  
  <p>Here are the current functions:</p>
  
  <pre><code>//PRINT BY LEVEL ORDER TRAVERSAL
  void Avltree::level_order(Avlnode* root, ofstream &amp;out){
  int h = height(root);
  for(int i = 0; i &lt; h; i++){
      out &lt;&lt; "Level " &lt;&lt; i &lt;&lt; ": "; 
      print_level(root, i, out);
      out &lt;&lt; endl;
  }
  }
  
  //PRINT A GIVEN LEVEL ON A TREE
  void Avltree::print_level(Avlnode* root, int level, ofstream &amp;out){
  
      if(root == NULL)
          return;
      if(level == 0){
          out &lt;&lt; root-&gt;data &lt;&lt; "(" &lt;&lt; height(root) &lt;&lt; ") ";
      }
      else if (level &gt; 0)
      {
          print_level(root-&gt;left, level-1, out);
          print_level(root-&gt;right, level-1, out);
      }
  }
  </code></pre>