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++ functions have a wide range of types. In this article, we are going to cover one such function: setw C++. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What is setw Function in C++?

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. 

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 in C++

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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 c++ 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. 

Unleash a High-paying Automation Testing Job!

Automation Testing Masters ProgramExplore Program
Unleash a High-paying Automation Testing Job!

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 c++ string. So, if the argument passed is less than the string's length, the entire string is returned. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

Choose The Right Software Development Program

This table compares various courses offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help learners make an informed decision about which course best suits their needs.

Program Name Automation Testing Masters Program Full Stack Developer - MEAN Stack Caltech Coding Bootcamp
Geo All All US
University Simplilearn Simplilearn Caltech
Course Duration 11 Months 11 Months 6 Months
Coding Experience Required Basic Knowledge Basic Knowledge Basic Knowledge
Skills You Will Learn Java, AWS, API Testing, TDD, etc. HTML, CSS, Express.js, API Testing, etc. Java, JavaScript, Angular, MongoDB, etc.
Additional Benefits Structured Guidance
Learn From Experts
Hands-on Training
Blended Learning Program
Learn 20+ Tools and Skills
Industry Aligned Projects
Caltech Campus Connect
Career Services
17 CEU Credits
Cost $$ $$ $$$$
Explore Program Explore Program Explore Program

Conclusion

In this article, we saw a setw() function in C++ 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 Post Graduate Program in Full Stack Web Development in collaboration with Caltech CTME. 

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

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449