2017-09-26 6 views
0

私のラボでは、出力値を値の文に続く列に表示する必要があります。列内の各値の左端の文字の整列

私が必要とするものの例。私はどちらかsetwはこのために使用するために間違ったことがあるか、それは通常

のようなものをもたらすので、私はそれが間違って使っている私は仮定していsetwよう

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

を使用しようとしています

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

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

私は、彼らがそれらのそれぞれの前に関係なく、一例では文「の...量」の長さもしなかったようにすべてが揃え右に値を作るために何を使用することができますか?

+0

言っhttps://stackoverflow.com/questions/14765155/how-can-i-easily-format-my-data-table-in-c –

+0

'std :: setw'を値ではなく文字列に使用します。タイプミスとして閉じる。 – LogicStuff

+0

正当化修飾子[std :: left、std :: right](http://en.cppreference.com/w/cpp/io/manip/left)を参照してください。 –

答えて

0

右揃えと左揃えがあるようです。それ以外は、使用するのが苦痛であるように見えます。

C++ iomanip Alignment

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