t.slez
t.slez

Reputation: 25

Formatting with setw() c++

I am trying to create an organized looking graph with the amount of money for each food under each month. I was able to line up the first row, but after that, something started going off. This is what I ended up with:Current Output

This is the code I tried playing around with the setw() but I can't seem to change it for the number after it puts out the first array element.

void sales_report() {
    for (int i = 0; i < 7; i++) {
        cout << setw(17) << months[i] << " ";
    }

    cout << endl << foods[0] << " ";
    for (int i = 0; i < 6; i++) {
        sumapple += apples[i];
        cout << setprecision(2) << fixed << setw(8) << money << apples[i] << " ";
    }
    cout << setprecision(2) << fixed << setw(8) << money << sumapple << endl;
    cout << foods[1] << " ";

    for (int i = 0; i < 6; i++) {
        sumoranges += oranges[i];
        cout << setprecision(2) << fixed << setw(7) << money << oranges[i] << " ";
    }
    cout << setprecision(2) << fixed << setw(7) << money << sumoranges << endl;
    cout << foods[2] << " ";

    for (int i = 0; i < 6; i++) {
        sumpears += pears[i];
        cout << setprecision(2) << fixed << setw(10) << money << pears[i] << " ";
    }
    cout << setprecision(2) << fixed << setw(10) << money << sumpears << endl;
    cout << foods[3] << " ";

    for (int i = 0; i < 6; i++) {
        sumtomatoes += tomatoes[i];
        cout << setprecision(2) << fixed << setw(7) << money << tomatoes[i] << " ";
    }
    cout << setprecision(2) << fixed << setw(7) << money << sumtomatoes << endl;
    cout << foods[4] << " ";

    for (int i = 0; i < 6; i++) {
        sumcherries += cherries[i];
        cout << setprecision(2) << fixed << setw(6) << money << cherries[i] << " ";
    }
    cout << setprecision(2) << fixed << setw(6) << money << sumcherries << endl << totalmonth;
    for (int i = 0; i < 6; i++) {
        total = apples[i] + oranges[i] + pears[i] + tomatoes[i] + cherries[i];
        cout << setprecision(2) << fixed << setw(9) << money << total << " ";
    }

}

Upvotes: 1

Views: 538

Answers (1)

user1593881
user1593881

Reputation:

You are using different std::setw() values in each of the for loops. Hence the misalignment. Use the same values.

Upvotes: 2

Related Questions