#includeusing namespace std; int main() { int num = 123; cout.width(10); //Sets width to 10 cout << num << endl; //Outputs with minimum width of 10 return 0; }
#includeIn this example, the width function is used in conjunction with the "setprecision" function to output a double value with a precision of 3 decimal places and a minimum width of 10. The "fixed" manipulator is also used to ensure that the value is output with a fixed number of decimal places rather than in scientific notation. Overall, the std::ostream width function is a useful tool for formatting output in C++ programs. It is a member of the iostream package in the standard library.#include using namespace std; int main() { double pi = 3.14159; cout << setprecision(3) << fixed; //Sets precision to 3 decimal places cout.width(10); //Sets width to 10 cout << pi << endl; //Outputs with 3 decimal places and minimum width of 10 return 0; }