-1

How do I stop setw() from creating space after an endl? My first loop here which displays my column headers works fine, but on my second loop (which displays data), I get a large blank space before it shows the first line. I have tried clearing the buffer and putting extra "endl"s in.

for (int i = 0; i < airports.size(); i++)
{

    cout << airports[i] <<setw(10);
}
cout << endl;

for (int i = 0; i < numAirprts; i++)
{
    for (int j = 0; j < numAirprts; j++)
    {
        cout << i;
        cout << setw(4);

        for (int k = 0; k < milesCost; k++)
        {
            cout << costArray[i][j][k] << setw(2) <<" ";
        }

    }
    cout << endl;
}

and this is an example of the output:

DEN       LAX        LV       PHX       SFA       SFO       SJC       SLC

         0  -1  -1  0 844  69  0  -1  -1  0  -1  -1  01026  72  0  -1  -1  0  -1  -1  0  -1  -1
1  -1  -1  1  -1  -1  1 231  23  1  -1  -1  1  -1  -1  1 344  39  1  -1  -1  1 581  57
2  -1  -1  2  -1  -1  2  -1  -1  2 256  21  2  -1  -1  2  -1  -1  2  -1  -1  2 362  29
3 586  65  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1
4  -1  -1  4  -1  -1  4  -1  -1  4  -1  -1  4  -1  -1  4 679  67  4  -1  -1  4 700  59
5  -1  -1  5  -1  -1  5 420  39  5  -1  -1  5  -1  -1  5  -1  -1  5  -1  -1  5 605  19
6  -1  -1  6  -1  -1  6  -1  -1  6  -1  -1  6  -1  -1  6  51  19  6  -1  -1  6  -1  -1
7 379  49  7  -1  -1  7  -1  -1  7  -1  -1  7  -1  -1  7  -1  -1  7 585  29  7  -1  -1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Dec 03 '17 at 18:51
  • Questions seeking debugging help ("why isn't this code working?") **must** include the shortest code necessary to reproduce the problem in the question itself. Questions without a clear problem statement are not useful to other readers. Please read how to create a [MCVE]. – tambre Dec 03 '17 at 18:52

1 Answers1

1

You could reset setw before endling, something like:

cout << setw(0) << endl;
Stefano Azzalini
  • 1,027
  • 12
  • 21