Right Justifying output stream in C++

You need to use std::setw in conjunction with std::right.

#include <iostream>
#include <iomanip>

int main(void)
{
   std::cout << std::right << std::setw(13) << "foobar" << std::endl;
   return 0;
}

Yes. You can use setw() to set the width. The default justification is right-justified, and the default padding is space, so this will add spaces to the left.

stream << setw(13) << yourString

See: setw(). You'll need to include <iomanip>.


See "setw" and "right" in your favorite C++ (iostream) reference for further details:

 cout << setw(13) << right << your_string;

Tags:

C++