setw() : Setting field width Using Cout in C++ Programming
- setw() is library function in C++.
- setw() is declared inside #include<iomanip>
- setw() will set field width.
- setw() sets the number of characters to be used as the field width for the next insertion operation.
Syntax :
setw(num)
- num is width to be set in order to insert next value.
Live Example 1 : Setting Width in C++
#include <iostream>#include <iomanip>usingnamespacestd;intmain(){cout<< setw (10);cout<<"Pritesh"<< endl;return0;}
- Now Length of String Pritesh is 7
- We have set field width 10 so it will utilizes 7 fields
- Remaining 3 fields are kept blank.
- Default Justification : Left
Live Example 2 : Setting Width in C++
#include <iostream>#include <iomanip>usingnamespacestd;intmain(){constint maxCount =4;constint width =6;int row;int column;for(row=1;row<=10;row++){for(column =1; column <= maxCount; column++){cout<< setw(width)<< row * column;}cout<< endl;}return0;}
Output :
12342468369124812165101520612182471421288162432918273610203040
Lượt xem (675)