【问题标题】:C++ aligning characters to display neatly with any input implementedC ++对齐字符以在实现任何输入的情况下整齐地显示
【发布时间】:2017-10-20 03:53:30
【问题描述】:

我无法将“:”和“$”与用户通过 cin 命令(即“numTShirts”)实现的任何输入保持一致。如果实施 10 及以下,它会保持一致,但折扣与任何其他输入都不合适。

![用 10 实现]http://prntscr.com/gzms3m

![与其他实现]http://prntscr.com/gzmsjx

    cout << "\n" << endl;
        cout << fixed;
        cout << "Thank you for your purchase.\n" << endl;
        cout << "You bought " << numTShirts << " T-shirts\n" << endl;

        cout << "Subtotal Total" << setw(5) << ": $ " << right << setprecision(2) << subTotal << "\n" << endl;
        cout << setprecision(0) << "Discount(" << discountPCT << "%)" << setw(7) << ": $ " << right << showpoint << setprecision(2) << discount << "\n" << endl;
        cout << setfill('-') << setw(35) << "-\n" << endl;
        cout << setfill(' ');
        cout << "Total" << setw(14) << ": $ " << right << showpoint << setprecision(2) << totalPrice << endl;

【问题讨论】:

    标签: c++ setw


    【解决方案1】:

    发生这种情况是因为折扣金额是可变的,因此空间会增加。

    正如您在示例中所示,当折扣为一位数时,对齐是所需的,否则会不同。

    由于 disoutn 最多可以是三位数字 (100%),那么我建议你们两个对输出进行三种格式设置。

    1 位数折扣、2 位数和 3 位数折扣。 通过使用条件语句,您可以在输出中显示相应的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多