Script started on Mon Mar 22 15:05:59 1999 You have new mail. pizza% cat formatting.cpp #include #include #include using namespace std; int main() { const float ZipPrice = 13.95; const float PhilPrice = 3.95; string initials = "wcj"; int NumZip = 10; int NumPhil = 5; float Total = 145.30; // show specific column set up cout << "|" << setw(8) << "Customer" << "|" << setw(9) << "# Zips" << "|" << setw(12) << "# Phils" << "|" << setw(9) << "Cost" << "|" << endl; cout << "|" << setw(8) << "--------" << "|" << setw(9) << "-------" << "|" << setw(12) << "--------" << "|" << setw(9) << "------" << "|" << endl; cout << endl; // string variables not handled the same unfortunately cout << '|' << setw(8) << initials << '|' << setw(9) << NumZip << '|' << setw(12) << NumPhil << '|' << setw(9) << Total << '|' << endl; cout << endl; // show column oriented formatting cout << "|" << setw(15) << "Customer" << "|" << setw(15) << "# Zips" << "|" << setw(15) << "# Phils" << "|" << setw(15) << "Cost" << "|" << endl; cout << "|" << setw(15) << "--------" << "|" << setw(15) << "-------" << "|" << setw(15) << "--------" << "|" << setw(15) << "------" << "|" << endl; cout << endl; // show handling of not enough space cout << "|" << setw(7) << "Customer" << "|" << setw(7) << "# Zips" << "|" << setw(7) << "# Phils" << "|" << setw(7) << "Cost" << "|" << endl; cout << "|" << setw(7) << "--------" << "|" << setw(7) << "-------" << "|" << setw(7) << "--------" << "|" << setw(7) << "------" << "|" << endl; cout << endl; // show fill char - persistent cout << setfill('*') << "|" << setw(15) << "Customer" << "|" << setw(15) << "# Zips" << "|" << setw(15) << "# Phils" << "|" << setw(15) << "Cost" << "|" << endl; cout << "|" << setw(15) << "--------" << "|" << setw(15) << "-------" << "|" << setw(15) << "--------" << "|" << setw(15) << "------" << "|" << endl; cout << endl; cout << setfill(' ') << "|" << setw(15) << "Customer" << "|" << setw(15) << "# Zips" << "|" << setw(15) << "# Phils" << "|" << setw(15) << "Cost" << "|" << endl; cout << "|" << setw(15) << "--------" << "|" << setw(15) << "-------" << "|" << setw(15) << "--------" << "|" << setw(15) << "------" << "|" << endl; cout << endl; // hack to line up the same - depends on knowing 3 initials will be there cout << '|' << setw(12) << " " << setw(3) << initials << '|' << setw(15) << NumZip << '|' << setw(15) << NumPhil << '|' << setw(15) << Total << '|' << endl; cout << endl; initials = "jkl"; NumZip = 5; NumPhil = 25; Total = 148.75; // do another line in the same table cout << '|' << setw(12) << " " << setw(3) << initials << '|' << setw(15) << NumZip << '|' << setw(15) << NumPhil << '|' << setw(15) << Total << '|' << endl; cout << endl; float val1 = 123.456; float val2 = 9876.5432; float val3 = 32.12345; float val4 = 1.34; cout << setfill('-'); cout << "|" << setprecision(6) << setw(12) << val1 << "|" << setw(12) << val2 << "|" << setw(12) << val3 << "|" << setw(12) << val4 << "|" << endl; cout << "|" << setprecision(5) << setw(12) << val1 << "|" << setw(12) << val2 << "|" << setw(12) << val3 << "|" << setw(12) << val4 << "|" << endl; cout << "|" << setprecision(4) << setw(12) << val1 << "|" << setw(12) << val2 << "|" << setw(12) << val3 << "|" << setw(12) << val4 << "|" << endl; cout << "|" << setprecision(3) << setw(12) << val1 << "|" << setw(12) << val2 << "|" << setw(12) << val3 << "|" << setw(12) << val4 << "|" << endl; cout << "|" << setprecision(2) << setw(12) << val1 << "|" << setw(12) << val2 << "|" << setw(12) << val3 << "|" << setw(12) << val4 << "|" << endl; return 0; } */ pizza% pizza% pizza% g++ -s formatting.cpp pizza% pizza% pizza% pizza% a.out |Customer| # Zips| # Phils| Cost| |--------| -------| --------| ------| |wcj| 10| 5| 145.3| | Customer| # Zips| # Phils| Cost| | --------| -------| --------| ------| |Customer| # Zips|# Phils| Cost| |--------|-------|--------| ------| |*******Customer|*********# Zips|********# Phils|***********Cost| |*******--------|********-------|*******--------|*********------| | Customer| # Zips| # Phils| Cost| | --------| -------| --------| ------| | wcj| 10| 5| 145.3| | jkl| 5| 25| 148.75| |-----123.456|-----9876.54|-----32.1235|--------1.34| |------123.46|------9876.5|------32.123|--------1.34| |-------123.5|--------9877|-------32.12|--------1.34| |---------123|----9.88e+03|--------32.1|--------1.34| |-----1.2e+02|-----9.9e+03|----------32|---------1.3| pizza% ^Dexit script done on Mon Mar 22 15:06:40 1999