Thread: format output

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    format output

    hi all!

    I have a question, maybe a stupid one, but I really don't get the reason why this thing is happening..
    I have some data I want to print to my C++ program's output in columns,so I use tabs the way below: (Linux environment)

    Code:
    cout<<"User's nickname  is:\t"<<nickname<<"\tUser's id is:\t"<<id<<"\tUser's name is:\t"<<name<<endl;
    Now the strange thing is that the first tab comes as a white space,
    the other 2 tabs are ok and the tab "\tUser's name is
    comes out as a tab for some of users or as a white space for the rest of them....Why is that?Is there another way to format the output instead of tabs?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Because that is the pretty much the definition of a tab?

    What are you trying to do exactly?

    Are you trying to get all your data to line up? The tab character alone is not sufficient for that. You'll have to do some counting and use `std::setw(int)' or similar.

    Soma

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by phantomotap View Post
    O_o

    Because that is the pretty much the definition of a tab?

    What are you trying to do exactly?

    Are you trying to get all your data to line up? The tab character alone is not sufficient for that. You'll have to do some counting and use `std::setw(int)' or similar.

    Soma
    I want all my data to be in 3 columns ,one for nicknames ,one for ids and one for names
    I thought that was what tab does..

  4. #4

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Nope.

    The tab character says "Position the next character at the next nearest tab inset."

    If your string is a multiple of the tab inset the tab character will insert "tab inset" spaces in a text view.

    If your string is less than a multiple of the tab inset the tab character will insert spaces until the next character would line up with the nearest next tab inset.

    If the lengths of any two strings to be aligned at specific insets differ by more than tab inset spaces the tab character is insufficient.

    Like I said, if you want nicely aligned data columns you'll have to do some counting.

    Soma

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    116
    thank you guys!

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    116
    Actually I did use the setw as below but

    cout<<"User's id is:"<< setw(10) <<user.id<< setw(10) <<"User's host is:"<<setw(10)<<user.host<<setw(10)<<"User's full name is :"<<setw(5)<<user.name<<setw(10)<<user.nickname<<e ndl;

    and it doesn't even see some of the setw..It gives me that:

    User's id is: 1000User's host is :hostname User's full name is: NameNickname

    Why is that??

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    2 things
    1) setw never truncates the output
    2) default alignment is right.
    Kurt

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In other words, there's no point using setw(10) to print a string that's longer than 10 characters, as it's not allowed to add a negative number of spaces.
    Read the documentation on how it works.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output format to file
    By Niels_M in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2012, 02:22 PM
  2. How to format print for Hex like output
    By Teropita in forum C Programming
    Replies: 2
    Last Post: 05-07-2011, 06:59 PM
  3. output format
    By Beowolf in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2007, 01:22 AM
  4. output format: screen vs .txt
    By -JM in forum C++ Programming
    Replies: 7
    Last Post: 04-20-2006, 08:05 PM
  5. format numbers output?
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2006, 06:54 AM