Formatting I/O

lcoolsingh
lcoolsingh used Ask the Experts™
on
Hello all,

I have a simple question regarding how to perform some formatting in c++. I know you can use setw(xx) or width(), but when i have the following case below, things get out of hand:

Example:

Text              10
MoreText               10.

Basically i want to able to line things up, so in the above example i would like the number 10 to be lined in the same column as the one before, but this does not seem to be happening. I've currently got code like this:

cout << "Text" << setw(10) << 10<< endl;
cout << "MoreText" << setw(10) << 10<< endl;

Is their a way round this? I know the reason why this is happending, but dont know of a way to control formatting of this nature.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
Commented:
Hi lcoolsingh,
> cout << "Text" << setw(10) << 10<< endl;
> cout << "MoreText" << setw(10) << 10<< endl;

    cout << setw(20)<< "Text" << setw(10) << 10<< endl;
    cout << setw(20)<< "MoreText" << setw(10) << 10<< endl;

Cheers!
sunnycoder

Author

Commented:
But that means that the first ends up like this:

         Text            10
  MoreText            10

I think what needs to be done is to use setiosflags(ios::left).

thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial