Updated April 14, 2023
Introduction to C++ setw() Function
Like the different functions in C++ the setw() function helps in setting the field width which will be used on output operations. This function will take the member width whenever it will be called as an argument. It will need a stream where this field will be inserted or manipulated. This function will set the width parameter of the stream out or stream in to exactly n times. It will take the parameter which will be the new value of the width to which this has to be set.
Syntax:
setw(num)
The setw() function can be used with an expression, while the output can be set by using out << setw(num) and it can be taken as an input as well by using in >> setw(num).
How setw() Method Works in C++?
The setw() function helps in setting the width of the field with the number specified in parenthesis. The below code will help us understand this better.
Code:
#include <iostream>
#include <iomanip>
int main () {
std::cout << std::setw(10);
std::cout <<546<< std::endl;
return 0;
}
Output:
_ _ _ _ _ _ _ 5 4 6
Explanation: In the above code we understand that we have used the standard libraries. One which has an input-output stream and the other is the input-output manipulation library to which setw() function belongs. This function will not work unless this library is imported. The main function then uses the setw() function for setting the width for the output field. We set it to 10 and then output the number as 546. It will first have this field reset to 0 and when the number is specified it is set to 10. It will work in the below manner.
It will keep the first 7 places blank as the length of the string is three. The last 3 places are written with the numbers that we have used as an output.
Examples to Implement C++ setw() Function
We will now see some examples which will help us understand this function better.
Example #1
Setting width from 0 to a number and then resetting it.
Code:
// Example to see the working of std::setw manipulator
#include <iostream>
#include <iomanip> // this library stored the std::setw
int main()
{
// set initial width of 10
std::cout << std::setw(10);
std::cout << 500 << std::endl;
std::string str = "EduCBA";
// changing width to 12
std::cout << std::setw(12);
std::cout << str << std::endl;
return 0;
}
Output:
Explanation: The above code helps us in understanding the working of setw function. We have imported the iomanip library. After that, we have set the value to 10 for this field. It resets itself from 0. We then send a number as an output. Later we increase the width by using this setw() function again. Before that we have declared a string str. We change the width and then send this string as our new output. It will display both outputs and you can observe the change in the spacings as well. Please see the above screenshot which helps us in understanding the program better.
Example #2
Setting the width lesser than the number of characters used in the output.
Code:
#include <iostream>
#include<iomanip>
#include<string>
using std::cout;
using std::string;
using std::endl;
int main() {
string shrtstr="Hello EduCBA! Lets set the width";
cout<<std::setw(5)<<shrtstr<<endl;
return 0;
}
Output:
Explanation: The above program is similar to the one in Example 1. We have declared a variable shrtsrt which stored a string. We have set the width using setw() function to 5. Here the length of the string is quite bigger than the string itself. The specaility of setw() function is that it will not truncate the string as the length or width defined is less. It will display the entire string and set the with to the default value which is 0. Though the argument sent to the function is lesser than the string and the output is having more width, the function will still display the entire string as the output. Please check the output above to understand better.
Example #3 – setw() function with for loop
We can also use this function along with looping functions as below:
Code:
#include
#include
using namespace std;
int main()
{
const int maxDisplayCount = 5;
const int width = 7;
int row;
int i;
for(row=1;row<=5;row++)
{
for(i = 1; i <= maxDisplayCount; i++)
{
cout << setw(width) << row * i;
}
cout << endl;
}
return 0;
}
Output:
Explanation: In the above program we have used a for loop along with setw() function. Here we have taken a constant integer which is set to 5. We have also declared a variable which has its value set to 7. In the for loop we have put the condition of row being less than 5 and the send for is checking the iterations till it reaches the value of maxDisplayCount. The inner for will keep running until the valu of ‘i’ reaches 5. It is multiplying these values. Before multiplying these values it is setting the width each time before sending the output of this multiplication to the screen. This helps in setting the tab by making use of this function. There is no need of specifying ‘\t’ explicitly as setw() function does this job. The outer for will keep running until the row variable reaches 5. Both these for will keep running and give us the desired output which will be each number and its multiples till they reach 5. The output will be systematic due to the use of setw() function every time before displaying the result. Below will be the output of above function which shows that setw() function can be used easily with looping structures.
Conclusion
The setw() function is a part of the iomanip library which contains the manipulator functions. It helps in setting the width of an output or input field. This function will not truncate the string even if the defined width is lesser than the length of the string. The setw() function makes the code and output readable and systematic which helps in getting a formatted output.
Recommended Article
This is a guide to C ++ setw(). Here we discuss the Introduction of C ++ setw() Function and its Examples along with Code Implementation and Output. you can also go through our suggested articles to learn more –