1

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.

Aaron Thomsen
  • 45
  • 1
  • 5

1 Answers1

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

instead of using '\n' fixes that.

See the Live Demo.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks, it works. This crossed my mind briefly, but the answers I found said the only difference between '\n' and endl was that endl cleared the output buffer. Why does this work? – Aaron Thomsen Feb 01 '17 at 21:21
  • @JesperJuhl I'm still [investigating about that](http://coliru.stacked-crooked.com/a/efd69a553d1eaba6). If you have a good one feel free to chime in. – πάντα ῥεῖ Feb 01 '17 at 21:23
  • 1
    Perhaps the fact that "Points Scored\n" is exactly 14=20-6 characters long should ring a bell. – n. m. could be an AI Feb 01 '17 at 21:26