c++indentationcoutmanipulators

C++ setiosflags function manipulator - undetermined indentation


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

Solution

  • 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.