Output Formatting No, I don't want 6 digits…
Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros: prints as 1.5 – Use 6 digits : prints as – Scientific notation for large/small numbers: prints as e+09
Output and Formatting Output cout accepts expressions or manipulators – if expression, value is printed – if manipulator, output format is modified
cout options Manipulators : special values that tell cout how to do its job cout << "Hey – show 2 decimal places" << ; Basic manipulator : endl Others in iomanip library #include
showpoint Manipulator showpoint forces output to show the decimal point and trailing zeros Example: cout << 15.0 << " " << showpoint << 15.0 << " "; cout << 4.0 << endl; Persistent
fixed Manipulator Doubles output in floating-point format: cout << << endl; e+009 fixed forces output as fixed decimal points: cout << fixed; cout << << endl; Persistent
fixed Manipulator scientific manipulator – Return to floating point format cout << fixed; cout << << endl; cout << scientific; cout << << endl; e+009 Persistent
setprecision Manipulator setprecision(n) – Number of decimal places before value is rounded cout << fixed << setprecision(2); cout << << " "; cout << << endl; Persistent
setw setw(x) – Makes next expression take up at least x columns Adds spaces to left cout << 5 << setw(5) << 12 << endl; – Longer expressions not affected: cout << 5 << setw(2) << 12123;
setw setw(x) – Applies only to next expression cout << setw(6) << 12 << 12; cout << setw(6) << 12 << setw(6) << 12; NOT Persistent
Additional Output Formatting Tools setfill(char) – What char to use instead of spaces to fill columns left and right manipulators – Which side to justify text cout << left << setfill('*'); cout << setw(5) << 12 << 10; 12***10 Persistent
Goal Read in 2 numbers – numerator, denominator Print something like this: Enter numbers: 2 3 Numerator/Denominator Decimal Value 2/3 0.67
Goal Visualize spaces: Enter numbers: 2 3 Numerator/Denominator..Decimal Value / Break into pieces: : right, width of : left, width of : 2 decimal places, fixed
Finished Program