• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Printing Columns in C++

I need to know how to print columns in C++.  I have to print a very large Heap (5000  in it) and I need to know how to format it in 6 different columns to look something like this:

Col 1    Col 2  Col 3  Col 4  Col 5   Col 6
xxxx  xxxx  xxxx   xxxx xxxx   xxxx
etc.

Right now I have it printing on the same line and because there are so many items in the Heap I can't read all of them. Could somebody please give me some ideas on this?
0
huskerwoman
Asked:
huskerwoman
1 Solution
 
huskerwomanAuthor Commented:
My code to print looks like this:

for(int m=0; m<edgesAccepted.size(); m++)
{
cout << "(" << edgesAccepted.top().vertex1 << " " << edgesAccepted.top().vertex2 << ")" << endl;
      edgesAccepted.pop();
}

My heap size is 5000.  It doesn't matter which order they go, or what goes in what column. I just don't want it to print 5000 different lines! Thanks
0
 
Bob LearnedCommented:
Did you try using the tab character \t?

Bob
0
 
huskerwomanAuthor Commented:
No I have not, can you use it in a loop? If you see my loop above I am printing the queue item after item.  I am not sure how to use the tab item.  How would you tell it to go to the next line after it has printed my 6 columns?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ozoCommented:
   cout << "(" << edgesAccepted.top().vertex1 << " " << edgesAccepted.top().vertex2 << ")" ;
      if( m%6==5 ){
        cout << endl;
      }else{
        cout << "\t";
      }
0
 
Bob LearnedCommented:
Another question is the "endl" at the end of the line.  Is this the entire loop or an inner loop?

Bob
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Preceed each item with << "\t".

cout << "(\t" << edgesAccepted.top().vertex1 << " \t" << edgesAccepted.top().vertex2 << ")" << endl;

If the variation in length of the items in any column is large enough to span the tab width, the contents of the line to the right of the tab will be shifted by one or more tabs.  You could even have several wide columns on one line.

If you absolutely need each column aligned (not mostly aligned like tabs will provide) you'll need to force the width of each column's output (perhaps using substr() or other function).  You can also use the lower level C routines for formatted output (like printf).


Good Luck,
Kent
0
 
huskerwomanAuthor Commented:
Thanks Bob, the \t character worked!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now