Thread: cout formatting

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    41

    cout formatting

    Trying to some simple column formatting.....

    Basically producing a table, where the headers display the name of the values stored in the tables.

    i.e. file << "KT"<< "\t" << "A" << "\t" << "E" << "\t" << "A_Norm" << "\t" << "E_Norm" << "\t" << "A/(KT)*2".... etc etc

    then underneath the values

    KT << "\t" << A << "\t" << B << "\t" << C << "\t" << D << "\t" << E << "\t" << F << "\t" << G << "\t" << H << "\t" << I << endl

    However these columns dont seem to align. How can I force alignment...or set-up in nice neat columns?

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    i.e.

    Name Name2 Name3
    Value Value2 Value3

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can use "setwidth()" to set the width of each element you output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    I have tried the following:

    cout << "G. " << setw(8) << 34 << setw(8) << 45 << endl;

    after including

    #include <iomanip>

    as well, but no avail, I get the following on compile:

    error: ‘setw’ was not declared in this scope

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    setw() is in namespace std.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    ok but it still does not work!

    I am running with GNU c++ compiler.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What code did you test with?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you show the code that "doesn't work", and also describe how it doesn't work?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ok but it still does not work!
    The default is right justification, so you might try left justification.
    Code:
    cout << std::left << "G. " << setw(8) << 34 << setw(8) << 45 << endl;

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Things that you should do:
    • Use either std::setw() or a using statement such as
      Code:
      using namespace std;
      or
      Code:
      using std::setw;
      This goes for everything, including cout and left and endl.
    • Call setw() before you print your data, like this.
      Code:
      std::cout << std::setw(10) << string << std::endl;
      If you call it afterwards, it will have no effect.


    Code:
    cout << std::left << "G. " << setw(8) << 34 << setw(8) << 45 << endl;
    Using std::left but not std::cout and std::setw and std::endl implies that the code used something like
    Code:
    using std::cout;
    using std::endl;
    using std::setw;
    // but not
    // using std::left;
    I think either
    1. the programmer won't care about namespaces, and just grab the whole thing with using namespace std; or
    2. the programmer will care enough about namespaces to put std:: in front of everything or use a using directive for each identifier.

    Besides which, it's inconsistent. And unimportant . . . .
    Last edited by dwks; 01-09-2008 at 03:31 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by dwks View Post
    Using std::left but not std::cout and std::setw and std::endl implies that the code used something like
    Agreed. I put std:: in there as an extra precaution, and to make left stand out, since the OP was having some issues with the compiler recognizing setw(), and it being good practice in larger programs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New, making a survey program
    By shaffer in forum C++ Programming
    Replies: 18
    Last Post: 12-01-2006, 11:36 AM
  2. "Spying" on cout
    By sirjis in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2006, 05:00 PM
  3. cout to printf problems
    By hallo007 in forum C++ Programming
    Replies: 9
    Last Post: 09-27-2006, 10:22 AM
  4. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  5. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM