0

对于我的实验室,我们需要在值的用途声明之后的列中显示输出值。

我需要的例子。

Amount of adult tickets:                      16
Amount of children tickets:                   12
Revenue from ticket sales:                    $140.22

我正在尝试使用 setw 之类的

cout << "Amount of adult tickets: " << setw(15) << ticketsAdult` << endl;
cout << "Amount of children tickets: " << setw(15) < ticketsChildren << endl;

我假设 setw 是错误的,或者我用错了,因为它通常会导致类似的结果

Amount of adult tickets:                    16
Amount of children tickets:                    12

我可以使用什么来使右侧的值都像示例中一样对齐,而不管它们之前的“Amount of ...”语句的长度如何?

4

2 回答 2

0

看起来也有左右对齐。除此之外,它看起来使用起来很痛苦。

C++ iomanip 对齐

于 2017-09-26T20:56:50.573 回答
0

将所说的一切结合在一起

#include <iostream>
#include <iomanip>

int main()
{
    int ticketsAdult = 16;
    int ticketsChildren = 12;
    double rev =140.22;
    std::cout << std::setw(35) << std::left << "Amount of adult tickets: " <<  ticketsAdult <<  '\n';
    std::cout << std::setw(35) << std::left << "Amount of children tickets: " <<   ticketsChildren <<  '\n';
    std::cout << std::setw(35) << std::left << "Revenue from ticket sales: " <<   '$' << rev <<  '\n';
    return 0;
}
于 2017-09-27T04:57:07.157 回答