2010年12月5日 星期日

3. Output Formatting

3. Output Formatting:
1. Field Width Setting field width is very simple. For each variable, simply precede it with "setw(n)". Like this:
#include 
#include iomanip

using namespace std;

int main()
{
const int max = 12;
const int width = 6;
for(int row = 1;row <= max;row++) {
for(int col = 1;col <= max;col++) {
cout << setw(width) << row * col;
}
cout << endl;
}
return 0;
}
Notice how "setw(n)" controls the field width, so each number is printed inside a field that stays the same width regardless of the width of the number itself.

沒有留言: