Friday, September 2, 2011

Right Align Your Output

Wondering how to change the alignment of the output?  Here is a little tip:
cout<<right is used for right alignment of your output. Similarly you can align it back to left with cout<<left. Don't forget to add the directive 'iomanip' in your code. See the sample below:

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace::std;
int main ()
{
          cout<<"How to right align your output \n"<<endl;
          cout<<right<<setfill(' ')<<setw(30);
          cout<<"123"<<endl;
          getch();
          return 0;
}

Here:
  • right is used to right align the output.
  • setw(30) is used to tell the computer how far you want to go from left. Here I have set it to 30 so 123 will be displayed 30 spaces away from left side.
  • setfill(' ') is used to to fill in the blank(between the left side of screen and the output) with any character you like. I have used a space here you can experiment with anything to see what it looks like.
Have a look at the screenshot of output you will get from above program. 




0 response(s):

Respond to this Post

Your participation is appriciated...