Printing Columns in C++

Posted on 2007-07-20
Last Modified: 2013-11-25
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

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?
Question by:huskerwoman

    Author Comment

    My code to print looks like this:

    for(int m=0; m<edgesAccepted.size(); m++)
    cout << "(" << << " " << << ")" << endl;

    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
    LVL 96

    Accepted Solution

    Did you try using the tab character \t?


    Author Comment

    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?
    LVL 84

    Expert Comment

       cout << "(" << << " " << << ")" ;
          if( m%6==5 ){
            cout << endl;
            cout << "\t";
    LVL 96

    Expert Comment

    by:Bob Learned
    Another question is the "endl" at the end of the line.  Is this the entire loop or an inner loop?

    LVL 45

    Expert Comment


    Preceed each item with << "\t".

    cout << "(\t" << << " \t" << << ")" << 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,

    Author Comment

    Thanks Bob, the \t character worked!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now