Results 1 to 5 of 5

Thread: formatting output *resolved*

  1. #1

    Thread Starter
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185

    formatting output *resolved*

    how do you get text to be printed in a specific column, like 20 spaces from the left? THat is, without just printing blank space like this " ", though, because I might have text before it that would affect the aligned text~!

    Code:
    some text           aligned at 20
                        more aligned at 20
    ',cr.pgp.l,eo       and again
    random stuff        but this is still aligned at 20!
    thanks in advance
    Last edited by DarkMoose; Jul 31st, 2002 at 11:13 AM.
    To understand recursion, one must first understand the concept of recursion.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <iomanip>
    
    // ...
    
    std::cout << std::setwidth(20) << "something" << "This is aligned" << std::endl;
    That should work
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Actually, it's setw(), not setwidth()
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    and that also won't entirely work:

    This is what I did:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main() {
    
    	string str1 = "fish";
    	cout << str1 << setw(40 - str1.length()) << "this is at 40" << endl;
    
    	str1 = "fungus";
    	cout << str1 << setw(40 - str1.length()) << "this is at 40" << endl;
    
    
    	return 0;
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    if you need an explaination or anything, let me know. It's pretty much self-explainitory.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width