Search code examples
c++iomanipsetw

Automatic spacing with iomanip


For the following code:

cout << left << setw(20) << "Example Header\n";
cout << setw(60) << setfill('-') << "-" << endl;

The second line (with the dashes) is indented by 5 spaces for some reason. I initially thought that something was automatically getting tabbed, but I can find no reason for why it would to that. No matter what goes on the second line, six spaces are automatically put in that I cannot remove. Even though a simple work around would be for me to just fill the second line with spaces, I am still curious to know why my program is putting these spaces in.


Solution

  • cout << left << setw(20) << "Name" << setw(20) << "Number" << setw(20) << "Points Scored" 
         << endl;
      // ^^^^^^^^
    

    instead of using '\n' fixes that.

    See the Live Demo.