setw() function in C++ with Examples
The setw() method of iomanip library in C++ is used to set the ios library field width based on the width specified as the parameter to this method. The setw() stands for set width and it works for both the input and the output streams.
- n: It is the integer argument corresponding to which the field width is to be set.
- This method does not return anything. It only acts as a stream manipulator.
Before setting the width: 50 Setting the width using setw to 5: 50
In the above example, we have used setw() to add padding to the integer output. We can use setw() for many such cases. Some of those are stated below.
Case 1: Using setw() with cin to limit the number of characters to take from the input stream.
Case 2: Using setw() to set the character limit for string output.
Increasing Width: GeeksforGeeks Decreasing Width: GeeksforGeeks
As the above example illustrate, we can increase the width of the string output using setw() but cannot decrease the output width of the string to less than the actual characters present in it.
My Personal Notes arrow_drop_up
Please Login to comment...