0

I am looking for a simple CPP solution to align a string to the right size.

What do I mean by this? here is an example:

the normal output would be

hello [message]
foo [message]

I would like it to output like this:

 hello [message]
   foo [message]

note: this is not for aligning a table but rather to know how to align a message by adding certain spaces to the left (or smth).

Maubg
  • 23
  • 1
  • 6

2 Answers2

0

In the library, you can find std:right and std:setw which can help to justify O/P to the right.

If 'i' is your string, then below is a snippet that you can use :-

#include <iomanip>

cout << right
     << setw(20)
     << fixed
     << i << endl;
10may
  • 311
  • 1
  • 2
  • 10
0

Like this ?

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setw(10) << "Hello" << " [message]" << std::endl ;
    std::cout << std::setw(10) << "foo" << " [message]" << std::endl ;
}
CGi03
  • 452
  • 1
  • 3
  • 8