对齐列中每个值的最左侧字符

时间:2020-12-18 13:09:22

For a lab of mine we need to display the output values in a column following a statement of what the value is for.

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

Example of what I need.

我需要的例子。

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

I am trying to use setw like

我想尝试使用setw

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

I'm assuming either setw is the wrong thing to use for this or I'm using it wrong as it usually results in something like

我假设setw是错误的用法,或者我使用它是错误的,因为它通常导致类似的东西

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

What can I use to make the values to the right all align like they did in the example no matter the length of the "Amount of..." statements before each of them?

我可以使用什么来使值向右都像在示例中那样对齐,无论它们之前的“Amount of ...”语句的长度如何?

2 个解决方案

#1


0  

It looks like there is right and left alignment too. Other than that it looks like it's a pain to use.

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

C++ iomanip Alignment

C ++ iomanip Alignment

#2


0  

Combining everything said together

把所有的东西结合起来

#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;
}

#1


0  

It looks like there is right and left alignment too. Other than that it looks like it's a pain to use.

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

C++ iomanip Alignment

C ++ iomanip Alignment

#2


0  

Combining everything said together

把所有的东西结合起来

#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;
}