首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用setw() c++格式化

用setw() c++格式化
EN

Stack Overflow用户
提问于 2017-02-07 23:52:24
回答 1查看 358关注 0票数 1

我正在尝试创建一个有组织的外观图,显示每个月的每一种食物的金额。我能排好第一排,但在那之后,有些事情开始发生了。这就是我最后的结局:

这是我尝试使用setw()的代码,但在它输出第一个数组元素之后,我似乎无法将它更改为数字。

代码语言:javascript
复制
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 << " ";
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 00:10:16

在每个std::setw()循环中使用不同的for值。因此出现了偏差。使用相同的值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42102202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档