Thread: setw() - left justify

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    setw() - left justify

    im new at programming and i know that setw(x) will justify everything to the right. how can i make everything left justified? i know there is a way b/c it mentions it in the book. but they dont give any examples and i have to write a program that formats the output left justified. thanks for your help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One way...
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s("Hello");
    
        cout << left << setw(10) << s << "there." << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    sweet! i didnt realize that it was that easy! i wonder y a begginers book would not show this way, but show something completely outrageaous. lemme get the book and write their method .................................................. .................................................. .................................................. ...............................................

    "you can left justify text output using the manipulator


    setiosflags(ios::left)"

    i didnt understand that. but the cout << left << setw(x) << command it so much easier!! just out of curiousity tho, how does the example the book mentions work? thanks for your help btw!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The example your text illustrates is the language from which the 'shortcut' version that hk_mp5kpdw provided is derived.

    Unless I'm mistaken, this is compiler-dependent, however. Borland supports it, though I'm not certain that MSVC++ does, for example. (I reformatted my hard drive recently and have yet to reinstall MSVC++. Just might do that in case other questions of this sort pop up. )

    Your source is not at all incorrect, so don't immediately start questioning its content. In fact, you may want to investigate further into the method your book demonstrates so that you know the foundations of the language rather than relying on shortcut versions that may not be portable.

    Btw, char data is left-justified by default while numeric data is right-justified. (Someone jump in and correct me if I've mixed the two up!) Something else to consider when you format your output, anyway.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. left justify
    By Max in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 06:32 PM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM