0

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

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

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?

Derek M
  • 29
  • 1
  • 4

2 Answers2

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

Sean Sherman
  • 327
  • 1
  • 16
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;
}
Artemy Vysotsky
  • 2,694
  • 11
  • 20