#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << left << setw(10) << "Hello"<< "*"
<< right << setw(4) << "World\n";
}

is the above code correct?? I think the alignments are not working for me..

MY output

Hello^^^^^^^^^*World (represented spaces as ^)

What output were you expecting?

actually, I'm not able to understand this topic!!

actually, I'm not able to understand this topic!!

Then read a book or ask a specific question.

Is your code correct? Yes, it compiles and runs nicely. If it doesn't do what you want because you don't know what you want or have any clue how the formatting modifiers work, the problem is obvious.

it is not working in files...
I want to right align the numbers (like phone numbers) after left alignment of names...
tried a lot... but not getting it.. :(

I believe you have misunderstood how setw works. setw sets the minimum width of the next field; if the field length is less than the set field width then the field is padded with spaces (or the given padding character) either to the left or right of the data until it is the minimum width.

So for left << setw(10) << "Hello" the minimum width is 10 but Hello is only 5 characters so 5 spaces are added to the field, they are added to the right hand side since you have said you want the data on the left.

However in right << setw(4) << "World\n" you have requested a minimum width of 4 but World\n is 6 characters, this is greater than the minimum width so no spaces are added since the field is already long enough.

If start by moving the \n out of the field and used the endl manipulator since you don't want to count the newline as part of the field and set the field width to something with greater than the size of World (5) say 10 like this

cout << left << setw(10) << "Hello"<< "*" << right << setw(10) << "World" << endl;

You get

Hello     *     World

Which is probably what you are after.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.