0

我无法使“:”和“$”与用户通过 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;

在此处输入图像描述 在此处输入图像描述

4

1 回答 1

1

发生这种情况是因为折扣量是可变的,并且空间会随之增加。

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

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

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

于 2017-10-20T04:28:21.630 回答