Search code examples
c++iostream

C++ minimum string width


Suppose I have

string a = "foo";

How can I print a to the console using printf that ensure a minimum width, say, 10?


Solution

  • You can use setw manipulator to set the width of the output, like this:

    cout << setw(10) << a << endl;
    

    The iomanip header needs to be included in order for this to compile.