Examples of Using setw

Statement Output ( _ means blank)
1. cout << setw(4) << ans
<< setw(5) << num
<< setw(4) << "Hi";
2. cout << setw(2) << ans
<< setw(4) << num
<< setw(2) << "Hi";
3. cout << setw(6) << ans
<< setw(3) << "Hi"
<< setw(5) << num;
4. cout << setw(7) << "Hi"
<< setw(4) << num;
5. cout << setw(1) << ans
<< setw(5) << num;
_ _33_7132_ _Hi
337132Hi
_ _ _ _33_Hi_7132
_ _ _ _ _Hi7132
33_7132


Next page
Previous page
Go to Lesson 4 index.