Further iomanip confusion

Hi,

I'm not sure the proper terminology for this alignment in c++, but in MS Word, it's called right alignment. I want to get able to have text end at a certain spot on the right no matter what is printed. Example..
1
2
3
//................................This is some text.
//.....This is more text, but ends in the same spot.
//...................This is even more example text. 

Important note, that's not 3 separate lines. That's 3 possible outputs for one single line all ending at the same right margin no matter the length of the output.

Is this possible?

(and the periods shouldn't be shown in the program.)

Thank you.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

int main()
{
    const char* const text[] = { "This is some short text.",
                                 "This is more text, which is quite somewhat longer.",
                                 "This is some intermediate length text." };

    std::cout << std::setfill( '.' ) ; // comment this out if the periods shouldn't be shown

    std::cout << "deafult aligned\n\n" ;
    for( const char* cstr : text ) std::cout << std::setw(60) << cstr << '\n' ;

    std::cout << "\n\n" << "left aligned\n\n" << std::left ;
    for( const char* cstr : text ) std::cout << std::setw(60) << cstr << '\n' ;

    std::cout << "\n\n" << "right aligned\n\n" << std::right ;
    for( const char* cstr : text ) std::cout << std::setw(60) << cstr << '\n' ;
}

http://coliru.stacked-crooked.com/a/fa88a859985a4a2d
Last edited on
Topic archived. No new replies allowed.