iomanip setw() function in C++ with Examples
source link: https://www.geeksforgeeks.org/iomanip-setw-function-in-c-with-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- Last Updated : 11 Oct, 2019
The setw() method of iomaip library in C++ is used to set the ios library field width based on the width specified as the parameter to this method.
Syntax:
setw(int n)
Parameters: This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set.
Return Value: This method does not returns anything. It only acts as stream manipulators.
Example 1:
// C++ code to demonstrate
// the working of setw() function
#include <iomanip>
#include <ios>
#include <iostream>
using
namespace
std;
int
main()
{
// Initializing the integer
int
num = 50;
cout <<
"Before setting the width: \n"
<< num << endl;
// Using setw()
cout <<
"Setting the width"
<<
" using setw to 5: \n"
<< setw(5);
cout << num << endl;
return
0;
}
Before setting the width: 50 Setting the width using setw to 5: 50
Example 2:
// C++ code to demonstrate
// the working of setw() function
#include <iomanip>
#include <ios>
#include <iostream>
using
namespace
std;
int
main()
{
// Initializing the integer
int
num = 50;
cout <<
"Before setting the width: \n"
<< num << endl;
// Using setw()
cout <<
"Setting the width"
<<
" using setw to 10: \n"
<< setw(10);
cout << num << endl;
return
0;
}
Before setting the width: 50 Setting the width using setw to 10: 50
Reference: http://www.cplusplus.com/reference/iomanip/setw/
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK