a-z
a-z

Reputation: 1664

C++: right justified field ouput

I wanna output a matrix in right-justified fields of length 8 in C++.
Is there any facility to make that easy to code?

Upvotes: 0

Views: 2048

Answers (3)

kbyrd
kbyrd

Reputation: 3351

You can use std::right and std::setw to get right justified fields in iostream. The default padding char is space, but you can change it with setfill(). Also, right isn't strictly necessary as I believe it is the default, but it's nice to be explicit.

std::cout << std::right << std::setw(8) << data_var

Upvotes: 5

Grammin
Grammin

Reputation: 12205

Yes

 std::right

will right justify and

std::setw(8)

will set the field width to 8.

Upvotes: 1

Perhaps printf("%-8d", 1234); ?

Upvotes: 1

Related Questions