Thread: right align

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    right align

    how do you right align using <iostream.h>?

    i've tried:
    Code:
    ...
    cout.setf(ios::right);
    ...
    Code:
    ...
    cout.setf(ios::right);
    cout<<right;
    ...
    Code:
    ...
    cout<<setiosflag(ios::right)<<...;
    ...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The simple manipulator is easiest:
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
        cout<< right << setw(20) <<"Hello, world!"<<endl;
    
        return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    First, don't use header files with .h extensions. Use the equivalent header files, and read about the using directive here:

    http://www.cplusplus.com/doc/ansi/hfiles.html

    Second, output using cout is right aligned by default, but every field is the exact width of what you output, so the right side is ragged. All you have to do to right align output is set the width of all the fields the same:

    #include<iomanip> //for setw()
    #include<iostream> //for cout
    using namespace std;

    cout<<setw(25)<<"See Jane run."<<endl;
    cout<<setw(25)<<"See Jane fall down."<<endl;
    Last edited by 7stud; 06-04-2003 at 12:25 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by 7stud
    First, don't use header files with .h extensions. Use the equivalent header files, and read about the using directive here:

    http://www.cplusplus.com/doc/ansi/hfiles.html
    that would be helpful if i wasn't using an outdated compiler...
    Originally posted by 7stud
    Second, output using cout is right aligned by default, but every field is the exact width of what you output, so the right side is ragged. All you have to do to right align output is set the width of all the fields the same:
    i want a predefined right though...

    thanks prelude... i'll try that later (i can't do it now - it's on a school compiler and the professor asked me how to do it, but none of the ways i tried helps...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You could always try
    Code:
    cout.setw( 25 );
    cout<<"bah"<<endl;
    instead of
    Code:
    cout<setw( 25 )<<"bah"<<endl;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    but the setw(25) is only if the width of the console is 25 characters, i think

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    This might be old but I thought i'd help some of you out...

    If you have "cout << right << num1 << num2 << num3 << num4;

    I don't think num2,3,4, will have the right align. Doesn't seem to work that way. You have to separate:

    Code:
    cout << fixed << right << setw(4) << setprecision(2) << num1;
    cout << fixed << right << setw(4) << setprecision(2) << num2;
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. align syntax
    By mynickmynick in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2008, 04:54 AM
  2. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  3. Left Align
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2002, 07:47 PM
  4. how to align text?
    By kuyajay in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2002, 02:51 AM
  5. How to get text to align left when using width(X)
    By Darksbane in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 09:25 AM