C++ setiosflags function manipulator - undetermined indentation

210 Views Asked by At

I am studying C++ and I am focusing on cout manipulator functions.

By running the following code I get an indentation in the second line containing Gauthier.

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setw(10) << std::setiosflags(std::ios::left)
        << "Mathieu\n"
        << "Gauthier\n"
        << "Paul\n"
        << "Louis\n"
        << "Pierre\n"
        << std::endl;
    return 0;
}

Can someone explain to me what is happening? Why Gauthier is indented while the other names are not?

Mathieu
  Gauthier
Paul
Louis
Pierre

Program ended with exit code: 0
1

There are 1 best solutions below

7
On BEST ANSWER

std::ios::left tells to add fill characters to the right, i.e. it adds few characters to first string, so "Mathieu\n" "becomes" "Mathieu\n ". There is new line character at the end ('\n'), so added spaces are moved to next line (Gauthier). So it's not indentation of second line, those are trailing characters from first.