C++ Manipulators

The C++ manipulators are stream functions that modify the characteristics of input and output stream. It is used for formating the input and output stream by changing format flags and values for the stream.

The list of manipulator function is located in <iomanip.h> header file. You need to include this header to use the manipulator functions in your program.

The list of C++ standard manipulator functions is as follows.

  • endl
  • hex, oct, dec
  • setbase
  • setw
  • setfill
  • setprecision
  • ends
  • ws
  • flush
  • setiosflags
  • resetiosflags

The hex, dec, oct, ws, endl, ends, and flush are defined in stream.h header file. The rest are defined in iomanip.h header files.

endl

The endl introduce a new line or a line feed character. It is similar to C programming language “\n” character and C++ supports the old line feed.

For example,

cout << "This line use line feed" << endl;

cout << number1 <<  endl << number2 << endl;

You can use it anywhere and a new line character is added automatically.

Example Program #1

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;o

int main()
{
cout << " This is Accounting Software";
cout << endl;
cout << " The Author is Chang lee";
cout << endl;

system("PAUSE");
return EXIT_SUCCESS;
}

Output:

 This is Accounting Software
 The Author is Chang lee

setbase()

The setbase() manipulator is a manipulator that changes the base of a number to another base value. The C++ language supports following base values:

  • hex (Hexadecimal = 16)
  • oct (Octal = 8)
  • dec (Decimal = 10)

Other than the above base converters the setbase () can modify base of a variable. The hex, oct, and dec manipulator can modify base of input or output numbers.

For example,

int number = 100;
cout << "Hex Value =" << " " << hex << number << endl;
cout << "Octal Value=" << " " << oct << number << endl;
cout << "Setbase Value=" << " " << setbase(16) << number << endl;

The output of the above code is:

Hex Value =  0064
Octal Value =  144
Setbase Value= 0064

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //Variable Declaration
    
    int A,B,C;
    
    //Variable Initialization
    
    A = 2078;
    
    B = 3067;
    
    // Computing C
    
    C = A + B;
    
    // Printing Results 
    
    cout << "A =" << dec << A << endl;
    
    cout << "B =" << oct << B << endl;
    
    cout << "C = " << setbase(16) << C << endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

A =2078
B =5773
C = 1419

setw()

The setw() is an output manipulator that create whitespace character between two variables. You must specify the an interger value equal to the space required.

setw( int n)

For example,

cout << number1 << number2 << endl;

cout << setw(2) << number1 << setw(5) << number2 << endl;

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //variable declaration
    
    int number1, number2, total;
    
    //variable initialization
    
    number1 = 100;
    number2 = 345;
    
    // expression 
    
    total = number1 + number2;
    
    //printing output with setw
    
    cout << endl;
    cout << endl;
    cout << setw(5) << number1 << " + " << setw(5) << number2 << " = "  << setw(6) << total << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

100 +   345 =    445

setfill()

The setfill() fill the whitespaces of setw() with a different character. It is an output manipulator like setw(), but the required parameter is a single character. Note that a charater is enclosed in between single quotes.

setfill(char ch)

For example,

cout<< setfill('*') << endl;
cout << setw(5) << number1  << setw(5) << number2 << endl;

The output of the above will be ‘*’ character between variable number1 and variable number2.

Example Program:

We will use the above setw() example with slight modifications.

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //variable declaration
    
    int number1, number2, total;
    
    //variable initialization
    
    number1 = 100;
    number2 = 345;
    
    // expression 
    
    total = number1 + number2;
    
    //printing output with setw
    
    cout << endl;
    cout << endl;
    cout << setfill('*') << endl;
    cout << setw(5) << number1 << " + " << setw(5) << number2 << " = "  << setw(6) << total << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

**100 + **345 = ***445

Note the asterisk between variables due to setfill() manipulator.

setprecision()

The setprecision() is an output manipulator which controls the number of digits to display for a floating point number after the decimal. The function is defined in the ipmanip header so make sure to include this file in your program.

For example,

float A = 1.34255;
cout << setprecision(3) << A << endl;

The output will be 1.34.

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //variable declaration
    float number1;
    
    //variable initialization
    
    number1 = 34.3358;
    
    //display the number using setprecision()
    cout << number1 << endl;
    cout << setprecision(2) << number1 << endl;
    cout << setprecision(3) << number1 << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

34.3358
34
34.3

ends

The ends adds a null terminating character(‘\0’) to a string type value. It has no argument like endl; It just adds a null.

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    // variable declaration
    
    int amount;
    
    // variable initialization
    
    amount = 330;
    
    //display as a string
     
     cout << " \" " << amount << ends << " \" " << endl;
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

 " 330  "

ws

The ws  is an input stream manipulator that discard any white spaces, therefore, if you have a string with multiple words then the string will display only the first word and trim after that.

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    // variable declaration
    
    char name[125];
    
    // read variable value
    
    cout << "Enter Name" << endl;
    cin >> ws;
    cin >> name;

    
    //display variable with whitespace
     
     cout << "The Name  is "<< name << endl;
     
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Enter Name
George Land
The Name  is George

Flush

The flush function clears the output stream. It is an output manipulator means does not work for input stream. The output buffer for console screen is emptied automatically, but the flush is useful in clearing file output buffer after file operations.This function does not take any arguments.

Example Program:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    
    //using Flush manipulator
    
    cout << "The Beautiful World of Computer Science" << endl;
    cout << "Learn Programming First" << endl;
    cout.flush();
    cout << "Output Buffer Cleared" << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

The Beautiful World of Computer Science
Learn Programming First
Output Buffer Cleared

Setiosflags and Resetiosflags

The input and output stream has flag bits set by default. The setiosflags and resetiosflags helps control the behavior of both streams by modifying the stream flag bits.

The setiosflags will set a flag bit. Alternatively, you can use setf() function which does the same thing.

setiosflags(long n)

The resetiosflags will reset a flag bit, but ylou may use resetf() function.

resetiosflags( long n)

The C++ flags are discussed in more detail in the next lesson.

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag