/* PROGRAM output formatting samples AUTHOR: Nan Zhao FSU ID: nz08 COURSE INFORMATION: COP 3014 Recitation Number: 2 SUMMARY Samples showing outpput formats. INPUT N/A OUTPUT Sample outputs with different outpput formats. DATA STRUCTURES N/A ASSUMPTIONS N/A */ #include #include #include using namespace std; int main() { /************* variable declaration *************/ int int_num_1 = 36123, int_num_2 = -36123; double num_1 = 100000000, num_2 = 44.00, num_3 = 3.141592653; char ch = 'Q'; const char BLANK = ' '; const string FSU_NAME = "Florida State University"; /************* executable codes *************/ // Formatting output of integers cout << "====================================" << endl; cout << "=== Formatting output of integer ===" << endl; cout << "====================================" << endl << endl; // the case without any output formatting cout << "===== No output formatting =====" << endl; cout << "int_num_1 =" << int_num_1 << "and int_num_2 =" << int_num_2 << endl << endl; // turn on setw (right-justified in default) cout << "===== turn on setw: setw(10) =====" << endl; cout << "int_num_1 =" << setw(10) << int_num_1 << "and int_num_2 =" << int_num_2 << endl << endl; // flip to left-justified cout << "===== flip to left-justified =====" << endl; cout << left; cout << "int_num_1 =" << setw(10) << int_num_1 << "and int_num_2 =" << int_num_2 << endl << endl; // switch back to right-justified cout << "===== switch back to right-justified =====" << endl; cout << right; cout << "int_num_1 =" << setw(10) << int_num_1 << "and int_num_2 =" << int_num_2 << endl << endl; // setw is too small cout << "===== setw is too small: setw(3) =====" << endl; cout << "int_num_1 =" << setw(1) << int_num_1 << "and int_num_2 =" << int_num_2 << endl << endl << endl; // Formatting output of reals cout << "====================================" << endl; cout << "=== Formatting output of double ===" << endl; cout << "====================================" << endl << endl; cout << setprecision(10) << "num_1 = " << num_1 << endl; cout << fixed << setprecision(2); cout << "num_2 = " << num_2 << endl; cout.unsetf(ios::fixed); cout << setprecision(10) << "num_3 = " << num_3 << endl; cout << endl; cout << setprecision(6); // the case without any output formatting cout << "===== No output formatting =====" << endl; cout << "num_1 = " << num_1 << endl; // scientific notation cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // precision setting cout << "===== setting precision (9) =====" << endl; cout << setprecision(9); // precision setting (need: # include ) cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // turn off precision setting cout << "===== turn off precision setting (6) =====" << endl; cout << setprecision(6); // turn off precision setting cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // turn on showpoint cout << "===== turn on showpoint =====" << endl; cout << showpoint; // turn on showpoint cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // turn off showpoint cout << "===== turn off showpoint =====" << endl; cout.unsetf(ios::showpoint); // turn off showpoint cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // turn on fixed cout << "===== turn on fixed =====" << endl; cout << fixed; // turn on fixed cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl; // turn off fixed cout << "===== turn off fixed =====" << endl; cout.unsetf(ios::fixed); // turn off fixed cout << "num_1 = " << num_1 << endl; cout << "num_2 = " << num_2 << endl; cout << "num_3 = " << num_3 << endl; cout << endl << endl << endl; // Formatting output of char cout << "====================================" << endl; cout << "=== Formatting output of char ===" << endl; cout << "====================================" << endl << endl; // the case without any output formatting cout << "===== No output formatting =====" << endl; cout << '*' << ch << '*' << endl << endl; // turn on setw (right-justified in default) cout << "===== turn on setw: setw(10) =====" << endl; cout << setw(10) << BLANK << "(EOL)" << endl << endl << endl; // Formatting output of strings cout << "====================================" << endl; cout << "=== Formatting output of strings ===" << endl; cout << "====================================" << endl << endl; // the case without any output formatting cout << "===== No output formatting =====" << endl; cout << FSU_NAME << " (" << "FSU" << ")" << endl << endl; // turn on setw (right-justified in default) cout << "===== turn on setw: setw(6) =====" << endl; cout << FSU_NAME << " (" << setw(6) << "FSU" << ")" << endl << endl; // flip to left-justified cout << "===== flip to left-justified =====" << endl; cout << left; cout << FSU_NAME << " (" << setw(6) << "FSU" << ")" << endl << endl; // setw is too small cout << "===== setw is too small: setw(6) =====" << endl; cout << setw(6) << FSU_NAME << " (" << "FSU" << ")" << endl << endl; cout << endl << endl; return(0); }