C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Friday, 14 April 2017

C++ set () : Setting Field Width

                          C++ set () : Setting Field Width

Setw() : Setting field width using cout in C++ Programming:

  • setw() is library function in c++.
  • setw() is decleard inside #include<iomanip>.
  • setw() will set field width.
  • setw() sets the number if 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>

using namespace std;

int main ()

{  
    cout <<setw (10);
    cout <<"Aradhana" << endl;


    return 0;

}

1. Now Length of String Aradhana is 8

2. We have set field width 10 so it well utilizes 7 fields

3. Reamining 3 fields are kept blank.

4. Default Justification : Left


Live Example 2 : Setting Width in  C++  :


#include <iostream>
#include <iomanip>


using namespace std;

int main()
{

   const int maxCount = 4;
   const int width = 6;
   int row;
   int coloum;

for (row=1; row<=10;row++)

    for (column = 1; column <= maxcount;  column++)

     {
         cout << set (width) << row *column;
      }
    cout << endl;
}

 return 0;

}
      

Output :


1       2        3        4   
2       4        6        8
3       6        9       12
4       8        12     16
5       10      15      20
6       12      18      24
7       14      21      28
8       16      24      32
9       18      27      36
10     20      30      40 













0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget