Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
C++ is an enhanced version of C language, one of the most popular programming languages supporting object-oriented programming. It has many inbuilt libraries and functions, providing a lot of functionality and making the life of programmers easy.
One such function is setw which is present in the iomanip library of C++. It is used to set the ios field's width.
Don't worry if these terms seem new to you; we will discuss them in great detail in this blog. So without any delays, let's dive into the topic.
What is setw Function in C++?
Among the many functions that are inbuilt in C++, one such function is setw which is present in the iomanip library of C++. It is used to set the ios field's width. By the width of the ios field, we mean the width of the input-output stream. Stream is a data sequence that either flows from the device to the main memory (input) or from the main memory to the device (output). The setw function is a stream manipulator in C++, which means that it is used to manipulate and alter the characteristics of the input and output stream.
Working of setw in C++
The setw c++ function stands for set width and is used to set the ios field width., i.e., the number of characters in the output should be at least equal to the width. And if the length of the output string is less than that then padding is added to the output stream. By padding, we mean that extra space will be added to the output stream if the length of the output string is less than the specified width.
Another use of the setw function is that it is used to put a limit on the length of input that we take from the user.
This thing will become more clear when we illustrate this with an example.
Syntax-
std::setw(int n);
You can also try this code with Online C++ Compiler
The `setw` function in C++ is a manipulator that sets the field width for the next output operation. It is defined in the `<iomanip>` header file. The `setw` function takes a single integer argument that specifies the minimum number of characters to be written for the next output.
Let's discuss the parameters and use of the `setw` function in detail:
setw(int n)
You can also try this code with Online C++ Compiler
`n`: The minimum number of characters to be written for the next output. If the output is shorter than `n`, padding characters are added to reach the specified width. If the output is longer than `n`, the full output is written without truncation.
Examples:
1. Right-aligning output:
#include <iostream>
#include <iomanip>
int main() {
int num = 42;
std::cout << std::setw(5) << num << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
In this example, `setw(5)` sets the field width to 5 characters. The output `42` is right-aligned within the field, and padding spaces are added on the left to reach the specified width.
2. Left-aligning output:
#include <iostream>
#include <iomanip>
int main() {
std::string text = "Hello";
std::cout << std::left << std::setw(8) << text << "World" << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
In this example, `std::left` is used to left-align the output. `setw(8)` sets the field width to 8 characters for the `text` output. The text "Hello" is written, and padding spaces are added on the right to reach the specified width. The "World" output follows immediately after the padding spaces.
3. Filling with specific characters:
#include <iostream>
#include <iomanip>
int main() {
double value = 3.14159;
std::cout << std::setfill('*') << std::setw(10) << value << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
In this example, `std::setfill('*')` sets the fill character to an asterisk ('*'). `setw(10)` sets the field width to 10 characters. The `value` is written, and the padding characters (asterisks) are added on the left to reach the specified width.
Always remember that the `setw` function only affects the next output operation. If you want to set the field width for multiple outputs, you need to use `setw` before each output operation.
Name Age City
-----------------------
Rahul 25 London
Alice 30 Paris
Bob 20 New York
In this example, `setw` is used to create a formatted table. The field widths are set for each column using `setw`, and `std::left` is used to left-align the output. The `std::setfill('-')` is used to fill the second row with dashes to create a separator line.
In this example, `setw(10)` sets the field width to 10 characters, and `std::setfill('*')` fills the padding characters with asterisks. `std::fixed` and `std::setprecision(2)` are used to format the floating-point number with a fixed number of decimal places.
3. Centering output:
#include <iostream>
#include <iomanip>
#include <string>
int main() {
std::string text = "Centered Text";
int width = 20;
int padding = (width - text.length()) / 2;
std::cout << std::setw(padding) << "" << text << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
In this example, the `setw` function is used to center the output within a specified width. The padding is calculated by subtracting the length of the text from the desired width and dividing it by 2. `setw(padding)` is used to add the necessary padding spaces before the text.
Application of the setw function in C++ programs
To limit the width of the input stream.
We use the setw function in C++ to set the width of the input stream so that only a specified number of characters can be taken as input from users. For example, if we set the width of the input stream as ten, then only the first ten characters will be taken from the input stream.
Implementation
#include <iostream>
#include <iomanip> // setw function is present in this library
using namespace std;
int main() {
string sample;
cin>>setw(5); // This sets the width of the input stream as 5.
cin>>sample; // To Take input from user
cout<<sample<<endl; //Printing the result
}
You can also try this code with Online C++ Compiler
In the above example, we have included the iomanip library in our program as it is the one that contains the setw function.
Then we declare the string named sample.
We were using the setw function with the cin statement. We set the width of the input stream as 5.
Then we take input from the user, and the input string is “HelloNinja” of length 10. But the input stream length is 5; hence only Hello is stored in the sample.
Then we output the value stored in the sample.
Now you might be wondering what will happen in a case where the length of the string in the input stream is less than the maximum defined length. In that case, the program will run as it is. The effect of the setw will not be visible in that case.
To limit the width of the output stream.
We also use the setw C++ function to set the width of the output stream so that at least a minimum specified number of characters will be there in output string. And if the length of the output string will be less than that then extra padding will be done to the string.
Implementation
#include <iostream>
#include <iomanip> // setw function is present in this library
using namespace std;
int main() {
string sample="Ninja Bob";
cout<<"Output without setting the width of field:\n"<<sample<<endl;
cout<<"Output after setting the width of field:\n";
cout<<setw(15)<<sample<<endl; //Setting the width of output stream as 15.
}
You can also try this code with Online C++ Compiler
Output without setting the width of field:
Ninja Bob
Output after setting the width of field:
Ninja Bob
Explanation-
In the above example, we have included the iomanip library in our program as it is the one that contains the setw function.
Then we declare the string named sample and initialize it with “Ninja Bob.”
We first output the string without setting the width of the output stream.
Then we set the width of the output field as 15. And the length of the string is 9, which is less than the minimum length of the output field. Therefore extra padding has been added to the string output.
There can be a case where the length of the output string is more than the minimum defined length. In that case, the program will run as it is. The effect of the setw will not be visible in that case.
The setw C++ function is used to set the width of the input-output stream. By the width of the input-output stream, we mean the number of characters that the user can output or take input. setw function puts a lower limit on the size of the output stream and an upper limit on the size of the input stream.
Can we restrict the size of the input stream using setw?
Yes, we can restrict the size of the input stream using the setw C++ function. It is used to set the width of the input stream. By doing that, we ensure that the input stream's size will be equal to or less than that length. The excess part of the input will be ignored if the input string length exceeds that length.
Can we restrict the size of the output stream using setw?
Yes, we can restrict the size of the output stream using the setw C++ function to set the width of the output stream. By doing that, we ensure that the output stream's size will be equal to or greater than that length. Padding is added to the output stream if the output string is less than the specified width.
Conclusion
In this article we learned about the setw function in C++ and its various applications. We explained how to effectively utilize the setw function to format and align output in your code. We also talked about the function's parameters and how they control the field width and padding of the output. Lastly, we explained the limitations of setw, like its impact being restricted to the next output operation only.