Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Setw function in C++
3.
Working of setw in C++
4.
Application of the setw function in C++ programs
4.1.
To limit the width of the input stream.
4.2.
To limit the width of the output stream.
5.
Frequently Asked Questions
5.1.
What is the use of setw()?
5.2.
Can we restrict the size of the input stream using setw?
5.3.
Can we restrict the size of the output stream using setw?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Setw Cpp

Author Rohit Kumar
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

setw C++

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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);

 

Parameter- An integer denoting the value that will be assigned as the width of the field.

Return type- The setw C++ function has no return type, and it just acts as a stream manipulator.

 

example of setw


Also see, Abstract Data Types in C++

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.

Code-

#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
}


Input-

HelloNinja

 

Output-

Hello 

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.

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.

Code-

#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.
}

 

Output-

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. 

Also Read - C++ Interview Questions and Abstract Data Types in C++

Frequently Asked Questions

What is the use of setw()?

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

Congrats, Ninja. You've learned about the setw C++ function and what are the uses of the setw function. You have also learned how can we use the setw C++ function in our code and what are the limitations of the setw function.

We also added some FAQs related to the setw function.

Also read -  File Handling in CPP

Thanks for reading, and please upvote if you liked the article.

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSand  System Design, etc. as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!!

 

Live masterclass