• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Manipulators in C++
Next →
← Prev

Manipulators in C++

By Dinesh Thakur

The unformatted I/O statements that it is impossible to display output in a required user format or input the values in the desired form. In certain situations, we may need to format the I/O as per user requirements. For example :

1) The square root of a number should be displayed upto 2 decimal places.
2) The number to be inputted must be in a hexadecimal form.

To overcome the problems of unformatted I/O operations in C++, the concept of manipulators was introduced.

The manipulators in C++ are special functions that can be used with insertion (<<) and extraction (>>) operators to manipulate or format the data in the desired way. Certain manipulators are used with << operator to display the output in a particular format, whereas certain manipulators are used with >> operator to input the data in the desired form. The manipulators are used to set field widths, set precision, inserting a new line, skipping white space etc. In a single I/O statement, we can have more than one manipulator, which can be chained as shown

cout<<manipl<<var1<<manip2<<var2;
cout<<manipl<<manip2<<var1;

To use most of the manipulators, we need to include the header file iomanip.h in the program.

ManipulatorPurposeHeader File
endlcauses line feed to be inserted i.e. ‘\n’iostream.h
dec,oct,hexset the desired number systemiostream.h
setbase (b)output integers in base biomanip.h
setw(w)read or write values to w charactersiomanip.h
setfill (c)fills the whitespace with character ciomanip.h
setprecision(n)set floating point precision to niomanip.h

Now we shall discuss the above manipulators in detail.

Note: One should note that the manipulators endl, setw(), setfill() can be used with all types of data, whereas manipulators setbase(), Dec, Oct, hex are used with integer data types. Also, setprecision() manipulators used only with the float data type.

Another thing to note is that while using the manipulators with arguments (like setw() etc.). You need to include iomanip.h header file, whereas while using manipulators without arguments(oct, hex, endl, etc.), you need to include iostream.h header file.

We’ll be covering the following topics in this tutorial:

  • endl Manipulator
  • Dec, Oct,Hex Manipulator
  • setbase(b) Manipulator
  • setw(w) Manipulator
  • setfill(c) Manipulator
  • setprecision(n) Manipulator

endl Manipulator

The endl manipulator stands for endline and is used with an insertion operator (<<) that moves the cursor to the next line. If we do not use endl, the next output will be displayed in the same line. The endl has the same function as that of ‘\n.’

#include<iostraam.h>
#include<conio.h>
int main() {
 cout<<"Entar name"<<endl;
 cout<<"Myname is Thakur";
 qetch();
 return 0;
}
Output
Enter name
Myname is Thakur

Dec, Oct,Hex Manipulator

All the numbers are displayed and read in decimal notation by default. However, you may change the base of an integer value to octal or hexadecimal or back to a decimal using the manipulator’s oct, hex or dec, respectively. These manipulators are preceded by the appropriate variables to be used with.

#include<iostream.h>
#include<conio.h>
int: main() {
 int i;
 cout<<"Enter hexadecimal number =";
 cin>>hex>>i;
 cout:<<"Hexadecimal value = "<<hex<<i<<endl;
 cout<<"Octal Value = "<<oct<<i<<endl;
 cout<<"Dcimal Value = "<<dec<<i<<endl;
 getch();
 return 0;
}
Output :
Enter hexadecimal = f
Hexadecimal = f
Octal value = 17
Decimal value = 15

In the above program, the variable i is inputted in hexadecimal form using hex manipulator in cin statement. To display the number in different notations like hexadecimal, octal and decimal, the manipulator’s hex, oct and dee are used.

The base of a number remains the same until changed explicitly. For example, suppose the base of a number i of integer type is changed to hexadecimal (hex) during output. In that case, it will display the output only in hexadecimal form until being specified explicitly using dee or oct manipulators.

setbase(b) Manipulator

The setbase () manipulator is used to change the base of a numeric value during inputting and outputting. It is an alternative to Dec, Oct and hex manipulators. It is a function that takes a single integer argument(b) having values 8, 10 or 16 to set the base of the numeric value to octal, decimal and hexadecimal, respectively. The default base is 10.

#include<iostream.h>
#include<iomanip.h>
int main() {
 int num;
 cout<<"Enter number in Octal form = ";
 cin>>setbase(8)>>num;
 cout<<"Value of number in decimal form = "<<setbase(10)<<num<<endl;
 cout<<"Value of number in octal form = "<<setbase(8)<<num<<endl;
 cout<<"Value of number in hexadecimal form = "<<setbase(l6)<<num;
 return 0;
}
Output
Enter numberin Octal form = 21
Value of number in decimal fonn = 17
Value of number in octal fonn = 21
Value of number in hexadecimal fonn = 11

In the above program, the value of variable num is inputted in octal form using setbase(8) manipulator in the cin statement. The value of the variable num is displayed in different number systems by using setbase(b) manipulator with arguments values 10, 8 and 16.

setw(w) Manipulator

The setw() stands for set width. It is a function that takes a single integer argument which specifies the amount of space used to display the required value. We typically use the setw() manipulator for displaying output so that it becomes more understandable. It can be used to format only one value at a time.

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
int main() {
 int age = 22,rollno = 910l;
 cout<<setw(l2)<<"My Rollno is"<<setw(8)<<rollno<<endl;
 cout<<setw(l2)<<"My Aqe is"<<setw(8)<<age;
 getch();
 return 0;
}

manipulator in C++In the above program, the setw() manipulator causes the number or string that follows it in the cout statement to be displayed within the specified width. The value to be displayed is right-justified. The values of variable rollno and age are displayed within 8 spaces specified using setw(S) manipulator. The value is padded with leading blank spaces as it doesn’t fill the whole width.

If the value is larger than the specified width, it will not truncate the value, and the value is printed as it is. For example

cout<<setw(2)<<"Roll is"<<setw(2)<<rollno;

the output is

manipulator in C++

setfill(c) Manipulator

The setfill() manipulator is used in conjunction with the setw() manipulator. The compiler leaves the empty spaces on the left side of the required value if the set width is greater than the needed space. If you wish to fill the blank space with an alternative character instead of a blank space, you can use the
setfill () manipulator.

Generally, you will not want to change the fill character. However, one common example of when you may want to is creating a program that prints cheques. To prevent the cheque amount from being altered by the user, computer-generated cheque amounts are usually printed with leading asterisks(*). This is done in C++ with a setfill() manipulator.

The setfill () manipulator takes a single character as an argument and fills the empty spaces with the specified character c on the left of the value displayed if the width specified using setw() manipulator is greater than the value to be displayed.

#inclucle<iostream.h>
#include<iomanip.h>
#include<conio.h>
int main() {
 int age = 22,rollno = 910l; cout<<setfill('#');
 cout<<setw(4)<<age<<setw(6)<<rollno<<endl;
 cout<<setw(6)<<age<<setw(8)<<rollno;
 getch();
 return 0;
}
Output :
##22##9101
####22####9101

setprecision(n) Manipulator

The setprecision() manipulator is used to control the precision of floating-point numbers, i.e. the number of digits to the right of the decimal point. By default, the precision of the floating-point number displayed is 6. This precision can be modified by using a setprecision () manipulator. This function takes an integer argument n that specifies the number of digits displayed after the decimal point. The floating-point number will be rounded to the specified precision.

#include<iostream.h>
#includi<iomanip.h>
#inelude<eonio.h>
int main() {
 float a = 129.455396;
 cout<<setprecision(2)<<a<<endl;
 cout<<setprecision(3)<<a;
 getch();
 return 0;
}
Output :
129.46
129.455

In the above program, the setprecision (2) rounds the number a to 2 decimal places after the decimal point, i.e. 129.46. Similarly, for others.

You’ll also like:

  1. Escape Sequences and Manipulators in c++
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW