Iomanip c что это

от admin

Name already in use

cpp-docs / docs / standard-library / iomanip.md

  • Go to file T
  • Go to line L
  • Copy path
  • Copy permalink
  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents

Copy raw contents

Copy raw contents

Include the iostreams standard header <iomanip> to define several manipulators that each take a single argument.

Each of these manipulators returns an unspecified type, called T1 through T10 , that overloads both basic_istream<Elem, Tr>::operator>> and basic_ostream<Elem, Tr>::operator<< . For more information, see operator>> and operator<< .

Name Description
get_money Obtains a monetary amount, optionally in international format.
get_time Obtains a time in a time structure by using a specified format.
put_money Provides a monetary amount, optionally in international format.
put_time Provides a time in a time structure and a format string to use.
quoted Enables convenient round-tripping of strings with insertion and extraction operators.
resetiosflags Clears the specified flags.
setbase Set base for integers.
setfill Sets the character that will be used to fill spaces in a right-justified display.
setiosflags Sets the specified flags.
setprecision Sets the precision for floating-point values.
setw Specifies the width of the display field.

Footer

© 2023 GitHub, Inc.

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

C++ Iomanip

Example 1: Iomanip Setbase() Method in C++

The basefield flag ios library is set by the setbase() function based on the parameter provided as the method’s input. It accepts the integer argument base which corresponds to the base that is to be set as a parameter. The setbase() function allows us to change the basefield of a numeric value to a different base.

We add the “iomanip” module for the usage of the setbase() method inside our code. We use the setbase() method in the main method. We set the decimal base by calling “10” as input and print the decimal base value of the number “100”. Just like this, we implement the base Hexa and octal inside the setbase() method and display the specified value in the given bases.

There, we have three base values generated from the specified base fields.

Example 2: Iomanip Setiosflags() Method in C++

The format flags of the ios library provided as the method’s parameter are set using the setiosflags() function of the iomanip library in C++. The “format_flag” is a parameter that this method accepts. There is nothing returned by this procedure. Only the stream manipulators are used by it.

After including the iomanip package, we employ the main method which we call the setiosflags method with the std command. Before that, we initialize the numerical value “80” in the variable “MyNum”. Then, we invoke the setiosflags method which triggers the showbase and uppercase flags for the specified number.

Here, the output prints the showbase and uppercase flags value in hex format.

Example 3: Iomanip Setw() Method in C++

The width that is passed in as a parameter to the setw() method of the C++ iomanip library is used to adjust the ios library field width. Now, with the program implemented, this method will be clearer.

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std ;

cout << "Breadth,before using the setw : \n "
<< val << endl ;
cout << "Breadth,after "
<< " using setw to 4: \n "
<< setw ( 4 ) ;
cout << val << endl ;

We utilize the iomanip library for the setw() method here. Then, within the program’s main method, we declare and initialize the integer value “val” with the value “40”. We print the variable “val” before deploying the setw() method. After this, we use the setw() method and set the parameter value “4” which is the breadth of the value “40”.

The output shows the functionality of the setw() method in the following:

Example 4: Iomanip Setfill() Method in C++

Using the character provided as an argument, the setfill() function of the C++ iomanip library sets the fill character for the iOS library. Consider the following program:

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std ;

We insert the library “iomanip” into the header section. Then, we enter the program’s main. We define the variable “number” and initialize it with the value “90”. First, we utilize the setw() method to set the width of the value “90”. Then, we invoke the setfill() method which occupies the width space with the symbol “*”.

The output shows the width of the value “90” and displays the filled symbol “*” on the width.

Example 5: Iomanip Setprecision() Method in C++

The floating point precision of the ios library is set using the setprecision() procedure of the C++ iomanip library based on the precision supplied as the method’s parameter.

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std ;

int main ( )
{
double decimal_num = 2.718281828459045 ;

cout << "Before Precision method: \n "
<< decimal_num << endl ;
cout << "Precision using"
<< " setprecision to 3: \n "
<< setprecision ( 3 ) ;
cout << decimal_num << endl ;
cout << "Precision using"
<< " setprecision to 7 : \n "
<< setprecision ( 7 ) ;
cout << decimal_num << endl ;
return 0 ;
}

After including the iomanip in the header section, we construct the program’s main. Here, we declare a variable “decimal_num” of the data type double and set the floating-point value to it. Then, we first print the value of the float number. After this, we reduce the setprecision() method. Inside the setprecision() method, we assign the value “3”. In the next setprecision() method, we assign the parameter value “7”.

In the output, the floating value is generated first. Next, a value is generated which has a precision set to “3” and only displays three values. Lastly, we set the precision value “7” so the floating value generated has only seven digits.

Example 6: Iomanip Get_Time() Method in C++

The specified format is used to extract a timestamp from a stream. A time structure containing the value of the parameter is returned. The parameter includes the time pointer and the format pointer.

#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std ;
int main ( )

With the addition of the ctime module along with the iomanip module, we implement the code for the get_time() method. Here, inside our main method, we construct the structure as “tm” which uses the “when” keyword. The time is provided by the user as get_time() and is set in the “cin” command. The get_time takes the “when” keyword reference and the “%R” as input. The fail() function is employed inside the if statement to throw errors upon failure in time or it executes the time in the specified format given in the cout command.

The time is inputted as “16:23” which is then converted into the format utilized in the code.

Example 7: Iomanip Get_Money() Method in C++

The get_money() method is employed to take the characters from the data stream being processed and translate them into a monetary statement, which is then saved as the value of money_amount.

#include <iostream>
#include <iomanip>

int main ( ) {
long double price_amount ;
std :: cout <> std :: get_money ( price_amount ) ;

if ( std :: cin . fail ( ) ) std :: cout << "Error getting amount readable \n " ;
else std :: cout << "The amount entered is: " << price_amount << ‘ \n ‘ ;
return 0 ;
}

We take the amount of the money from the user. When the user enters the money amount, it is stored in the get_money() method as we pass the variable “price_amount” inside it. After this, we utilize the if-else to generate two possibilities. If the amount is entered correctly, the if statement is executed. Otherwise, the else statement executes.

The program we just built is now run, and the outcome is as follows:

Conclusion

The C++ iomanip user manual is presented here. As its name implies, the iomanip library is a manipulation library that aids us in modifying the desired output. Using the methods in this library, we can acquire the result that we want. The introduction of C++’s iomanip and its various functions along with samples and code implementation, are covered in this article.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

C++ iomanip

By Aanchal SinghAanchal Singh

C++ iomanip

Introduction to C++ iomanip

The iomanip is a library in C++ which helps us in manipulating the output of any C++ program. There are many functions in this library that help in manipulating the output. To name a few we have functions to reset flags, set fill characters, set precision, get date and time, etc. It is a part of input-output library of the C++ standard library. All these functions can be easily used whenever they should affect the state of the iostream objects. We will take a look at all these functions in detail ahead.

Functions of C++ iomanip Library

We have 10 functions in the iomanip library. Let us check the syntax of each of them.

Web development, programming languages, Software testing & others

Python TutorialC SharpJavaJavaScript

C Plus PlusSoftware TestingSQLKali Linux

  1. resetiosflags: It clears the specified ios_base flags.
  2. setiosflags: It will set the ios_base flags.
  3. setbase: It will change the base which is needed as per the said base.
  4. setfill: It will fill with the character specified.
  5. setprecision: It will change the precision of any floating-point number.
  6. setw: It helps in changing the width of the next input or output field.
  7. get_money: It will segregate money value.
  8. put_money: It will format and generate an output which will be a monetary value.
  9. get_time: It will segregate the date or time value which will be sent in a specified format.
  10. put_time: It will format the date or time which will be providing as per the specified format.

Syntax of C++ iomanip

Let us check the syntax for these functions.

The mask is the flag which has to be set.

Here also the mask is the flag which has to be reset.

The base can be an integer to which the base is set.

This is a new fill character which is a character type variable that can be used to fill a string.

The integer here is the new value of the decimal precision.

Here the integer represents the number of characters that will be used as the width.

Here the first parameter will be the object where the monetary value will be stored. The second parameter will be a Boolean value.

Similarly, here also first parameter is the monetary value and second will be a Boolean value.

The first parameter here will be a pointer where the date and time will be stored. The second format will be the format of date and time in which the time is desired.

Similar to the above syntax, we will have a pointer to the structure and the second parameter will have a format in which the date or time is expected.

How iomanip Works in C++?

Let us check how this function works:

Code:

We have taken just one function for our understanding. We have taken a number where we are setting the precision. The iomanip library enables to change the precision. Initially, the precision is default and then we change it to 3 using the setprecision() function. This changes the precision value and the decimal number will not take only the first 3 decimal places. The output will be manipulated and the output will be displayed accordingly. This is because of the iomanip library.

Examples to Implement C++ iomanip

Below are the examples of C++ iomanip:

Example #1

Checking the examples of setprecision, setw, setfill, setiosflags and resetiosflags.

Code:

Output:

C++ iomanip Example 1

Explanation: The above program helps us understand 5 functions of iomanip library. We have imported the library iomanip which has manipulation functions present. We have used 5 functions. The first function is setprecision which helps in setting precision of the number defined. We have first set it to 4 and then to 9. We then set the width of the output field to 10. After this, we have used the setfill function where we are filling the number 15 with the character 15. We have set the width to 10. The first 8 characters are # then followed by 15. After this, we have used the setiosflags where we are setting the flag base. In a similar way, we can use resetiosflags function.

Example #2

Checking the example of get_time, put_time, setbase, get_money and put_money.

Code:

Output:

C++ iomanip Example 2

Explanation: In this program, we are using the remaining 5 functions from iomanip library. We have imported the iomanip library as before and set the base using the setbase function. After that we have used the get_money function and put_money function which takes the amount and print it as per the function. The get_time and put_time functions help us in displaying time in a given format. The output will be as below:

Conclusion

The iomanip library like the name suggests is a manipulation library that helps us in manipulating the output we want. We can use the functions in this library and get the desired output as we want.

Recommended Article

This is a guide to the C++ iomanip. Here we discuss the Introduction to C++ iomanip and its different functions along with Examples and Code Implementation. You can also go through our suggested articles to learn more –

# std::iomanip

When used in an expression out << setprecision(n) or in >> setprecision(n) , sets the precision parameter of the stream out or in to exactly n. Parameter of this function is integer, which is new value for precision.

# std::setfill

When used in an expression out << setfill(c) sets the fill character of the stream out to c .

Note: The current fill character may be obtained with std::ostream::fill .

# std::setiosflags

When used in an expression out << setiosflags(mask) or in >> setiosflags(mask) , sets all format flags of the stream out or in as specified by the mask.

List of all std::ios_base::fmtflags :

  • dec — use decimal base for integer I/O
  • oct — use octal base for integer I/O
  • hex — use hexadecimal base for integer I/O
  • basefield — dec|oct|hex|0 useful for masking operations
  • left — left adjustment(add fill characters to the right)
  • right — right adjustment (adds fill characters to the left)
  • internal — internal adjustment (adds fill characters to the internal designated point)
  • adjustfield — left|right|internal . Useful for masking operations
  • scientific — generate floating point types using scientific notation, or hex notation if combined with fixed
  • fixed — generate floating point types using fixed notation, or hex notation if combined with scientific
  • floatfield — scientific|fixed|(scientific|fixed)|0 . Useful for masking operations
  • boolalpha — insert and extract bool type in alphanumeric format
  • showbase — generate a prefix indicating the numeric base for integer output, require the currency indicator in monetary I/O
  • showpoint — generate a decimal-point character unconditionally for floating-point number output
  • showpos — generate a + character for non-negative numeric output
  • skipws — skip leading whitespace before certain input operations
  • unitbuf flush the output after each output operation
  • uppercase — replace certain lowercase letters with their uppercase equivalents in certain output output operations

Example of manipulators:

# std::setw

(where the last line is there to aid in seeing the character offsets).

Sometimes we need to set the width of the output field, usually when we need to get the output in some structured and proper layout. That can be done using std::setw of std::iomanip.

Читать:
Как запустить форму с помощью чекбокс

Похожие статьи