/* FILE: manipulators.cpp Examples of I/O stream manipulators Manipulators that don't take an arg (endl, flush, ...) are declared in Manipulators that do take an arg (setw(), setprecision(), ...) are declared in */ #include //for manipulators taking no args #include //for parameterized manipulators using namespace std; const char HT = '\t'; //Horizontal Tab int main(){ float x = 123.456789, y = 123.0; //default precision: 6 cout << "\ngeneral: " << x << HT //123.457 << y << endl; //123 cout << "showpoint: " << showpoint << x << HT //123.457 << y << endl; //123.000 cout << "scientific: " << scientific << x << HT //1.234568e+02 << y << endl; //1.230000e+02 cout << "fixed: " << fixed << x << HT //123.456787 << y << endl; //123.000000 cout << "setw(12): " << setw(12) << setfill('*') << x << HT //**123.456787 << setw(12) //setw is not persistent << y << endl; //**123.000000 //currency notation with manipulators cout << "currency: " << fixed << showpoint << setprecision(2) << "$" << x << HT << "$" << y << endl; //reset io flags resetiosflags(ios::fixed | ios::scientific | ios::showpoint ); //currency notation using an alternate manipulator //see p. 96 [Hennefeld] cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << "currency: " << "$" << x << HT << "$" << y << endl; //reset io flags resetiosflags(ios::fixed | ios::scientific | ios::showpoint ); //default precision: 6 cout << "\ngeneral: " << setprecision(6) << noshowpoint << x << HT //123.457 << y << endl; //123 return 0; }