#include // We have been using std::cout throughout this course. // // The identifier std::cout refers to an object representing // the standard output stream. Like other objects, std::cout // has member functions that can be called using the '.' // operator: // // object.member_function( arguments ) // // For example: // // std::cout.precision( 10 ); // // calls the member function 'precision' on the std::cout // object. // // Some useful member functions of std::cout include: // // precision: // https://en.cppreference.com/cpp/io/ios_base/precision // // width: // https://en.cppreference.com/cpp/io/ios_base/width // // fill: // https://en.cppreference.com/cpp/io/basic_ios/fill int main() { double pi{ 3.14159265358979323846 }; // precision(...) // // The precision controls the number of digits used when // displaying floating-point values. The default is 6. // // Unlike width(...), the precision remains in effect for // subsequent floating-point output until it is changed. std::cout << "Using the default precision:" << std::endl; std::cout << pi << std::endl; std::cout.precision( 10 ); std::cout << std::endl << "After std::cout.precision( 10 ):" << std::endl; std::cout << pi << std::endl; std::cout << pi/3.0 << std::endl; // Calling precision() with no argument returns the // current precision. std::cout << std::endl << "The current precision is " << std::cout.precision() << "." << std::endl; // width(...) // // The width specifies the minimum number of characters // to use for the next formatted output operation. // // Unlike precision(...), the width normally returns to // zero after that output operation. std::cout << std::endl << "Using std::cout.width( 10 ):" << std::endl; std::cout << "["; std::cout.width( 10 ); std::cout << 42; std::cout << "]" << std::endl; std::cout << "["; std::cout.width( 10 ); std::cout << 123456; std::cout << "]" << std::endl; // Notice that the width applied above does not continue // to apply to subsequent output. std::cout << "[" << 42 << "]" << std::endl; // fill(...) // // The fill character is used when the requested width is // greater than the number of characters being displayed. // // The default fill character is a space. std::cout << std::endl << "Changing the fill character:" << std::endl; std::cout.fill( '.' ); std::cout << "["; std::cout.width( 10 ); std::cout << 42; std::cout << "]" << std::endl; std::cout << "["; std::cout.width( 10 ); std::cout << 123456; std::cout << "]" << std::endl; // Calling fill() with no argument returns the current // fill character. std::cout << std::endl << "The current fill character is '" << std::cout.fill() << "'." << std::endl; // Restore the usual fill character. std::cout.fill( ' ' ); return 0; }