The objects fall into two groups: cin, cout, cerr, and clog are byte oriented, doing conventional byte-at-a-time transfers. Wcin, wcout, wcerr, and wclog are wide oriented, translating to and from the wide characters that the program manipulates internally. Because actually i'm doing something like a billboard for my program,the out put above is the first one,and will be follow by same output with a little difference,(the aeroplane move a bit to right)and then 3rd image.
Formatting output in C++, is important in the development of the output screen, which can be easily read and understood. C++ offers the programmer several input/output manipulators. Two of these (widely used) I/O manipulators are:
- setw()
- setprecision()
- Cout cout object with a stream of values flowing into it.
- I havent programmed in C for about 3 years. Since i started uni actually. Now i gotta implement some stuff for Algorithms and Complexity. I've got a vague idea of how to do that but thats not my problem. According to my memory and old programs that i dug up for my hard disk the following code.
![Count devices in device manager Count devices in device manager](/uploads/1/2/6/0/126098207/661291474.jpg)
In order to use these manipulators, you must include the header file named iomanip.h. Here is an example, showing how to include this header file in your C++ program.
The setw() Manipulator
In C++, the setw() manipulators sets the width of the field assigned for the output. It takes the size of the field (in number of characters) as parameter. Here is an example, this code fragment:
generates the following output on the screen (each underscore represents a blank space).
The setw() manipulator does not stick from one cout statement to the next. For example, if you want to right-justify three numbers within an 8-space field, you will need to repeat setw() for each value, as it shown below:
The output will be (each underscore represents a blank space):
C++ Formatting Output Example
Here are some example program demonstrating, how to format the output screen in C++
Here is the sample run of the above C++ program:
Here another type of C++ program, also demonstrating, output formatting in C++
Here is the sample run of this C++ program:
The setprecision() manipulator
In C++, the setprecision() manipulator sets the total number of digits to be displayed when floating-point numbers are printed. Here is an example, this code fragment:
will print the following output to the screen (notice the rounding) :
The setprecision() manipulator can also be used to set the number of decimal places to be displayed. In order for setprecision() to accomplish this task, you will have to set an ios flag. The flag is set with the following statement :
Once the flag has been set, the number you pass to setprecision() is the number of decimal places you want displayed. The following code:
generates the following output on the screen (notice no rounding):
Additional IOS flags
In the statement:
'fixed' i.e., ios::fixed is referred to as a format option. Other possible format options can be one of the following :
Format Value | Meaning |
---|---|
left | left-justify the output |
right | right-justify the output |
showpoint | displays decimal point and trailing zeros for all floating point numbers, even if the decimal places are not needed |
uppercase | display the 'e' in E-notation as 'E' rather than 'e' |
showpos | display a leading plus sign before positive values |
scientific | display floating point numbers in scientific ('E') notation |
fixed | display floating point numbers in normal notation - no trailing zeroes and no scientific notation |
You can remove these options by replacing setf(used with cout, recall cout.setf) with unsetf. For example, to get 5.8 to display as 5.80, the following lines of code are needed :
Please note that all the subsequent couts retain the precision set with the last setprecision(). That means setprecision() is 'sticky'. Whatever precision you set, sticks with the cout device until such time as you change it with an additional setprecision() later in the program.
< cpp | io | ios base
C++Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
I/O manipulators | ||||
C-style I/O | ||||
Buffers | ||||
(deprecated in C++98) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(deprecated in C++98) | ||||
(deprecated in C++98) | ||||
(deprecated in C++98) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Member functions | ||||
Formatting | ||||
Locales | ||||
Internal extensible array | ||||
Miscellaneous | ||||
Member classes | ||||
Member types |
streamsize precision()const; | (1) |
(2) |
Cout C Programming
Manages the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put.
1) Returns the current precision.
2) Sets the precision to the given one. Returns the previous precision.
The default precision, as established by std::basic_ios::init, is 6.
[edit]Parameters
new_precision | - | new precision setting |
[edit]Return value
the precision before the call to the function
[edit]Example
Output:
[edit]See also
![Download Download](/uploads/1/2/6/0/126098207/338271586.png)
C++ Cout Example
manages field width (public member function)[edit] |
changes floating-point precision (function)[edit] |
Dev C++ Cout
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/io/ios_base/precision&oldid=110525'