Home » Manipulators In C++ (endl, ends, setw, setprecision, setf)

Manipulators In C++ (endl, ends, setw, setprecision, setf)2 min read

by Abu Zar Mishwani
Manipulators In C++ (TechABU.co)

Manipulators are used for formatting output. The data is manipulated by the programmer’s choice of display. It doesn’t indicate that the value of a variable is changed; instead, it adjusts the I/O stream using the insertion (<<) and extraction (>>) operator.

There are several manipulators available in C++. The most commonly used manipulators are discussed below:

Manipulators Without Arguments:

The IOStream library provides the most important manipulators, which are described below.

#1. endl;

The endl manipulator has the same functionality as the “\n” newline character but with the difference of flushing the stream. It is defined in the ostream..

#2. ends;

It’s also defined in ostream, and it adds a null character to the output stream. When the linked output buffer has to be null-terminated to be processed as a C string, it usually works with std::ostrstream.

#3. ws; 

It is used to remove whitespaces in the string sequence and is defined in istream.

#4. flush;

It is also defined in ostream and flushes the output stream, which means it forces all output to be sent to the screen or to a file. The output would be the same if there was no flush, but it might not display in real time.

Manipulators with Arguments: 

Setw (20), setfill (‘*’), and a number of other manipulators are used with the argument. All of this is defined in the header file iomanip. We must include this header file in our program if we want to use these manipulators.

For example, you can select the minimum width and fill the empty area with any character you like using the manipulators below.

#1. setw

On output, this manipulator sets the minimum field width. The syntax of the setw manipulator is:

setw (x)

Setw causes the number or string that comes after it to be printed with a field of x characters wide, where x is the argument set in the setw manipulator. < iomanip.h > is the header file that must be included when using the setw manipulator.

#2. setfill (c)

It’s used to fill the output stream with the character ‘c.’

#3. setprecision (val)

It sets val as the new precision value for floating-point numbers.

#4. setiosflags(flag)

It’s used to set the format flags that the parameter mask specifies.

// manipulators in C++ example

#include <iostream>
#include <istream>
#include <sstream>
#include <iomanip>
#include <conio.h>
using namespace std;

int main (void)
	//endl manipulator
	cout << "Learning " << endl;
	cout << "C++ " << endl;
	cout << "With TechABU.co " <<endl;
	cout << " \n\n <!-----------------------------------------!>"<<endl;
	// ends manipulator
	cout<< "\n\n";
	 cout << "x ";
    // a NULL character will be added in the Output
    cout << "\ny" << ends;
    cout << "\nz" << endl;
    cout << " \n\n <!-----------------------------------------!>"<<endl;
    // ws emanipulator
    	cout<< "\n\n";
    istringstream str("		 I'm a Programming Enthusiast");
	string line;
	// Ignore all the whitespace in string
	// str before the first word.
	getline(str >> ws, line);

	// After printing the output it will automatically
	// write a new line in the output stream.
	cout << line << endl;
	cout << " \n\n <!-----------------------------------------!>"<<endl;

	//setw manipulator
	cout<< "\n\n";
	int  a,b;  
    a = 200;  
    b = 300;  
   cout << setw (5) << a << setw (5) << b << endl;  
   cout << setw (6) << a << setw (6) << b << endl;  
   cout << setw (7) << a << setw (7) << b << endl;  
   cout << setw (8) << a << setw (8) << b << endl;  
   	cout << " \n\n <!-----------------------------------------!>"<<endl;
   	//setfill manipulator
   	cout << "\n\n";
	    cout<< setfill('x');
	    cout<< setw(10) <<11<<"\n";
	    cout<< setw(10) <<222<<"\n";
	    cout<< setw(10) <<3333<<"\n";
	    cout<< setw(10) <<44444<<"\n";
	return 0;


With TechABU.co




I'm a Programming Enthusiast


  200  300
   200   300
    200    300
     200     300



Leave a Comment

Related Articles

Send this to a friend