You are reading the article How Setw() Method Work In C++? (Examples) updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Setw() Method Work In C++? (Examples)
Introduction to C++ setw() FunctionLike the different functions in C++ the setw() function helps in setting the field width which will be used on output operations. This function will take the member width whenever it will be called as an argument. It will need a stream where this field will be inserted or manipulated. This function will set the width parameter of the stream out or stream in to exactly n times. It will take the parameter which will be the new value of the width to which this has to be set.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
setw(num) How setw() Method Works in C++?The setw() function helps in setting the width of the field with the number specified in parenthesis. The below code will help us understand this better.
Code:
int main () { std::cout << std::setw(10); std::cout <<546<< std::endl; return 0; }
Output:
_ _ _ _ _ _ _ 5 4 6
Explanation: In the above code we understand that we have used the standard libraries. One which has an input-output stream and the other is the input-output manipulation library to which setw() function belongs. This function will not work unless this library is imported. The main function then uses the setw() function for setting the width for the output field. We set it to 10 and then output the number as 546. It will first have this field reset to 0 and when the number is specified it is set to 10. It will work in the below manner.
It will keep the first 7 places blank as the length of the string is three. The last 3 places are written with the numbers that we have used as an output.
Examples to Implement C++ setw() FunctionWe will now see some examples which will help us understand this function better.
Example #1Setting width from 0 to a number and then resetting it.
int main() { std::cout << std::setw(10); std::cout << 500 << std::endl; std::string str = "EduCBA"; std::cout << std::setw(12); std::cout << str << std::endl; return 0; }Output:
Explanation: The above code helps us in understanding the working of setw function. We have imported the iomanip library. After that, we have set the value to 10 for this field. It resets itself from 0. We then send a number as an output. Later we increase the width by using this setw() function again. Before that we have declared a string str. We change the width and then send this string as our new output. It will display both outputs and you can observe the change in the spacings as well. Please see the above screenshot which helps us in understanding the program better.
Example #2Setting the width lesser than the number of characters used in the output.
Code:
using std::cout; using std::string; using std::endl; int main() { string shrtstr=”Hello EduCBA! Lets set the width”; cout<<std::setw(5)<<shrtstr<<endl; return 0; }
Output:
Example #3 – setw() function with for loopWe can also use this function along with looping functions as below:
Code:
#include #include using namespace std; int main() { const int maxDisplayCount = 5; const int width = 7; int row; int i; for(row=1;row<=5;row++) { for(i = 1; i <= maxDisplayCount; i++) { cout << setw(width) << row * i; } cout << endl; } return 0; }Output:
Explanation: In the above program we have used a for loop along with setw() function. Here we have taken a constant integer which is set to 5. We have also declared a variable which has its value set to 7. In the for loop we have put the condition of row being less than 5 and the send for is checking the iterations till it reaches the value of maxDisplayCount. The inner for will keep running until the valu of ‘i’ reaches 5. It is multiplying these values. Before multiplying these values it is setting the width each time before sending the output of this multiplication to the screen. This helps in setting the tab by making use of this function. There is no need of specifying ‘t’ explicitly as setw() function does this job. The outer for will keep running until the row variable reaches 5. Both these for will keep running and give us the desired output which will be each number and its multiples till they reach 5. The output will be systematic due to the use of setw() function every time before displaying the result. Below will be the output of above function which shows that setw() function can be used easily with looping structures.
ConclusionThe setw() function is a part of the iomanip library which contains the manipulator functions. It helps in setting the width of an output or input field. This function will not truncate the string even if the defined width is lesser than the length of the string. The setw() function makes the code and output readable and systematic which helps in getting a formatted output.
Recommended ArticleThis is a guide to C ++ setw(). Here we discuss the Introduction of C ++ setw() Function and its Examples along with Code Implementation and Output. you can also go through our suggested articles to learn more –
You're reading How Setw() Method Work In C++? (Examples)
Update the detailed information about How Setw() Method Work In C++? (Examples) on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!