Programming and Graphics

(Kiana) #1

3.7 Formatted input and output 75


prints on the screen the pattern:


+---+---+---+---+
+---+---+---+---+
+---+---+---+---+

The code:


cout << setfill(’-’) << setw(20) << "-" <<endl;
cout << setfill(’.’) << setw(20) <<""<<endl;
cout << setfill(’=’) << setw(15) << "Thank you"
<< setw(5) << ""<< endl;
cout << setfill(’.’) << setw(20) <<""<<endl;
cout << setfill(’-’) << setw(20) <<""<<endl;

prints on the screen the pattern:


--------------------
...................
======Thank you=====
....................
--------------------

Table 3.7.1 presents I/O manipulators with brief descriptions. Some of
these manipulators apply to only one read or write, whereas others apply per-
manently until reset.


Formatting can also be implemented by the manipulators

setiosflags(ios::property1|ios::property2|...);

and


resetiosflags(ios::property1|ios::property2|...);

For example, the property attributeproperty1can befixedorscientific
(referring to notation),showpoint, or other. Thus, the commands:


pi=3.14159265358;
cout << setprecision(5) << setw(10);
cout << setiosflags(ios::scientific);
cout << pi << endl;

produce the output:


3.14159e+00
Free download pdf