demo-formatting - cout.setf(ios::showpoint); cout...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// This program demonstrates formatting options in C++ #include <iostream.h> #include <iomanip.h> // for setw(), setprecision() const double e = 2.7128; // base of the natural logarithm system int main() { // Some meaningless variables to play with. .. double a = 2.3173; double b = 3; double c = -0.71234567; // Let's look at some formatting options. .. cout << "\n\nFirst \"table\" of the variables and constants in the program:\n"; cout << "a:" << setw(10) << a << '\n' << "b:" << setw(10) << b << '\n' << "c:" << setw(10) << c << '\n' << "e:" << setw(10) << e << '\n'; cout << "\n\nSecond \"table\" of the variables and constants in the program:\n"; cout << "a:" << setw(10) << setprecision(3) << a << '\n' << "b:" << setw(10) << setprecision(3) << b << '\n' << "c:" << setw(10) << setprecision(3) << c << '\n' << "e:" << setw(10) << setprecision(3) << e << '\n'; cout << "\n\nThird \"table\" of the variables and constants in the program:\n";
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout.setf(ios::showpoint); cout &lt;&lt; &quot;a:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; a &lt;&lt; '\n' &lt;&lt; &quot;b:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; b &lt;&lt; '\n' &lt;&lt; &quot;c:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; c &lt;&lt; '\n' &lt;&lt; &quot;e:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; e &lt;&lt; '\n'; cout &lt;&lt; &quot;What if I want to print \'b\' without decimals? b: &quot; &lt;&lt; b &lt;&lt; '\n'; cout &lt;&lt; &quot;\noops. .. better turn off the showpoint flag. ..\n&quot;; cout.unsetf(ios::showpoint); cout &lt;&lt; &quot;b: &quot; &lt;&lt; b &lt;&lt; &quot;\n. ..ah, everything\'s back to normal!\n&quot;; cout &lt;&lt; &quot;\n\nFourth \&quot;table\&quot; of the variables and constants in the program:\n&quot;; cout.setf(ios::fixed); cout &lt;&lt; &quot;a:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; a &lt;&lt; '\n' &lt;&lt; &quot;b:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; b &lt;&lt; '\n' &lt;&lt; &quot;c:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; c &lt;&lt; '\n' &lt;&lt; &quot;e:&quot; &lt;&lt; setw(10) &lt;&lt; setprecision(3) &lt;&lt; e &lt;&lt; '\n'; cout.unsetf(ios::fixed); return 0; }...
View Full Document

This document was uploaded on 02/10/2011.

Ask a homework question - tutors are online