setw() function in C++

Today we will be learning what setw() function in C++ is and how it’s used.
This setw() function belongs to the iomanip library of C++.

Syntax: setw()

setw(int n)

  • n is the parameter integer up to which the width is to be set.
  • This function only acts as a stream manipulator and does not return anything.

Let’s discuss:

  • What is this function
  • How to use it in the program
  • The output of the program

Let’s see some examples then we will explain why and how does it work

Examples:

Let us see these examples with the help of the codes below in C++.

Example 1:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int x=10;
    cout<<"Before setting the width: "<<endl<<x<<endl;
    // using setw()
    cout<<"After applying the setw() function: "<<endl<<setw(5);
    cout<<x<<endl;
}

Output:

Before setting the width: 
10
After applying the setw() function: 
    10

Example 2:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int x=10;
    cout<<"Before setting the width: "<<endl<<x<<endl;
    // using setw()
    cout<<"After applying the setw() function: "<<endl<<setw(10);
    cout<<x<<endl;
}

Output:

Before setting the width:
10
After applying the setw() function:
          10

What is the setw() function?

setw() is a function in C++ that reserves a particular width in the output screen.
In easier terms:
setw() sets the width in the output screen in which output can be printed.
Inside the bracket, we pass a number that determines the width we are reserving

Let us see an example.
Say you want to go to a restaurant with 9 more friends.
You call and tell them that 10 of us are coming and we would like to book a table.
Now the restaurant holds a table for 10.
When you enter the restaurant, you go and sit on that table.

Now you are the programmer, the restaurant is the setw() function and the table for 10 is the width you are setting.
setw means set width.

It's syntax is:

cout<<setw(<width_size>);

Width size is a number. So if we put 10 in the brackets, we are reserving 10 fields.
But that is just reserving. What is it used for?
We can occupy the spots after by the following syntax.
cout<<set(<width_size>)<< <Message>;

Now the message gets filled in the blank space. Its default justification is left.
If you haven’t understood it yet, take a look at this example:

cout<<setw(10)<<"ABCDE";
o/p:
     ABCDE

ABCDE is 5 letters. But we are giving a width of 10. Hence the 5 blank spaces before A.

cout<<setw(10)<<"ABCDEF";
o/p:
    ABCDEF

Now here ABCDEF is 6 letters hence the 4 blank spaces before A.

Code Sample

Take a look at the following code:

#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
    int num;
    cout<<"Enter seats to be booked\n";
    cin>>num;
    int x;
    cout<<"How many of you have come?\n";
    cin>>x;
    int y = num-x;
    cout<<setw(num)<<string(y,'x')+string(x,'|');
    cout<<"\n";
    cout<<"We still have place for "<<y<<" people";
}

Let us take a look at it line by line.

  1. #include<iomanip>

    iomanip is the package we import to use the setw() function.
    Without iomanip, setw() is a pointless word.

  2. int num;
    cout<<"Enter seats to be booked\n";
    cin>>num;

    Here we are defining a variable num of type int.
    The program i.e. the restaurant asks you how many seats you want to be booked.
    The reply is stored in num.
    Assume we booked a table for 5.

  3. int x;
    cout<<"How many of you have come?\n";
    cin>>x;

    Now we are defining a variable x of type int.
    The restaurant asks you, how many have come for dinner and stores the reply in x.
    Assume out of the 5, 3 made it.

  4. int y = num-x;

    Now we are calculating the people who could not make it and storing it in y.
    So 5-3=2
    Hence 2 did not make it.

  5. cout<<setw(num)<<string(y,'x')+string(x,'|');
    cout<<"\n";
    cout<<"We still have place for "<<y<<" people";

    Now we are dedicating a width of the number of people we booked the table for.
    So 5 people were supposed to come, hence a width of 5 is reserved.
    Then we are printing ‘x’ for the people who could not come and ‘|’ for the people who could.
    Hence x gets printed twice and | gets printed 3 times.
    Lastly, we are telling the user that there is still a place there for y people, that is the place that has not been occupied.

Let us have a look at an example:

Enter seats to be booked
5
How many of you have come?
3
xx|||
We still have place for 2 people
Enter seats to be booked
5
How many of you have come?
4
x||||
We stil have place for 1 people

Hence we have covered what the setw() function is.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *