setw C++

C++ or CPP is a very common general-purpose programming language, that was developed as an enhanced version of the C language as it includes an object-oriented paradigm. It is a compiled and imperative language. C++ has many different functions. In this article, we are going to cover one such function: setw C++. 

What is setw Function?

setw C++ is a method of iomaip library present in C++. setw function is a C++ manipulator which stands for set width. The manipulator sets the ios library field width or specifies the minimum number of character positions a variable will consume. In simple terms, the setw C++ function helps set the field width used for output operations. The function takes member width as an argument and needs a stream where this field has to be manipulated or inserted. The function also sets the width parameter of the stream in or stream out exactly n times. The parameter it takes will be the new value that needs to be set as the width. 

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Syntax of setw C++

The syntax of the function is:

setw(int number)

where number is the number of characters to be set as the field width.

The setw C++ function is used with an expression generally. The output can be set using out<<setw(num), and input can be taken by using in>>setw(num). 

Parameters of setw Function

As shown above, the function only takes one parameter, which is an integer. This integer corresponds to the field width to be set. 

As the function acts only as a stream manipulator, it does not return anything.

Examples of setw Function

The following code will help you better understand the concept of setting the width of the field:

#include <iostream>

#include <iomanip>

int main () {

std::cout << std::setw(5);

std::cout <<123<< std::endl;

return 0;

}

Output: 

setw_C_Plus_Plus_1.

Explanation: This code uses standard libraries. One is the input-output stream, and another header file is the input-output manipulation library with the setw() function. The main function has the setw() function for setting the output field's width. We have set it to 5 and output the number 123. This function keeps the first two places blank as the length of the string is three. The last three places include the numbers we have used as the output. 

Let’s see how we can set width from 0 to a number and then reset it:

#include <iostream>

#include <iomanip> 

int main()

{

// initial width is set to 5

std::cout << std::setw(5);

std::cout << 123 << std::endl;

std::string str = "setw";

// changing width to 7

std::cout << std::setw(7);

std::cout << str << std::endl;

return 0;

}

Output: 

setw_C_Plus_Plus_2

Explanation: In the above code, we are importing and using the iomanip library. First, we set the value of the field to 5. The function resets the field width from 0 to 5. Then, the number is sent as an output. Later we have increased the width with the setw C++ function again. We are changing the width and sending string as the output. This returns both outputs and observes changes in spacing. 

Let’s see what happens when we set the width lesser than length of the output:

#include <iostream>

#include<iomanip>

#include<string>

using std::cout;

using std::string;

using std::endl;

int main() {

string str="setw in C++";

cout<<std::setw(7)<<str<<endl;

return 0;

}

Output:

setw_C_Plus_Plus_3

Explanation: In the above program, we have stored string str. The width has been set to 7 using the setw() function. The string's length is more than the length of the output. The specialty of the setw C++ function is that it does not truncate the string if the width of the field is less. Instead, it sets the default value to 0 and displays the entire string. So, if the argument passed is less than the string's length, the entire string is returned. 

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

Finally, let’s see an example that summarizes all the concepts:

#include <iostream>

#include<iomanip>

using namespace std;

int main()

{

    int n;

    cout<<"Enter number of seats you want to book\n";

    cin>>n;

    int p;

    cout<<"How many people are present?\n";

    cin>>p;

    int a = n-p;

    cout<<setw(n)<<string(a,'x')+string(p,'|');

    cout<<"\n";

    cout<<"There is place for "<<a<<" people";

}

Output:

setw_C_Plus_Plus_4

Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!

Conclusion

In this article, we saw a setw C++ function available in the iomanip library and a manipulator function. It can be used to set the width of the input or output field. The function does not truncate the string even if the specified length is less than the string's length. The setw C++ function helps get a formatted output and makes the output and code systematic and readable. If you are interested in learning more about C++ and web development, do not forget to check out Simplilearn’s full-stack web development certification

Simplilearn also offers a free learning platform called SkillUp, where learners can explore in-demand skills and take up free courses. These classes have been created by top practitioners and industry experts.So what are you waiting for? Start your learning journey now!

In case you have any questions for us, drop them in the comment section below and our experts will get back to you right away!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.