aherlambang
aherlambang

Reputation: 14418

formatting an integer output using ostream

I am trying to find the equivalence of %2d using cout << <<endl format. how can I do that?

Upvotes: 5

Views: 4955

Answers (2)

John Ledbetter
John Ledbetter

Reputation: 14183

The header <iomanip> contains the stream manipuator setw:

cout << setw(2) << myint << endl

You may also be interested in setfill:

cout << setfill('0') << setw(2) << myint << endl

will pad with a 0 instead of a space.

Upvotes: 14

codaddict
codaddict

Reputation: 455112

You need setw

Upvotes: 4

Related Questions