Getting Started with C++

Use of endl and setw

The use of C++ manipulators takes place for changing the format of the output. Furthermore, C++ manipulators refer to instructions to the I/O stream object that makes possible the modification of the I/O format in various ways. Also, a good example of manipulators in C++ is the setw manipulator.

setw manipulator

                                                                                                                               setw manipulator

endl C++ Manipulator

The word ‘endl’  in C++, a programming language, stands for end of line. Furthermore, the use of the endl C++ manipulators takes place to move the cursor to the beginning of the next line. Moreover, its working is similar to the ‘\n’ escape sequence.

cout<<”object”<<endl<<”Oriented”<<endl<<”Programming”;

Below is a sample program that shows the working of endl.

                    

#include

#include

using namespace std;

int main()

{

int num;

cout<<"Enter your roll number: ";

cin>>num;



cout<<"Hello roll number "<<num<<endl;

cout<<"Welcome to your new class!!";

getch();

Finally, return 0;

}

In the above program, the use of the endl operator takes place in the fourth statement inside main() function.
Furthermore, after this statement, the printing of the string “Welcome to your new class!!” takes place in the next line on the output screen.

Browse more Topics Under Getting Started with C++

 setw Manipulator

The word ‘setw’ in C++ stands for set width. Furthermore, the use of the setw C++ manipulator takes place to set the output’s field width on the output device. Moreover, by default, the displaying or printing of the output takes place right-justified within the specified field of setw C++ manipulators.

The use of the setw manipulator takes place to set the width of the output in a program. Furthermore, it takes up an argument ‘n’, the width of the field in which the displaying of the output is to take place. Moreover, the output in the field, by default, is right-aligned.

The general syntax of setw manipulator is as follows:

setw(n)

The ‘n’ indicates the field width that is the number of columns and it happens to be  an integer value. Also, if the specified field width is less in comparison to the width of the output data’s width that is to be displayed, then the effect of setw manipulator will not take place. Moreover, there will be a normal displaying or printing of the output.

The setw C++ manipulator is also a component of “iomanip.h” header file. Furthermore, the inclusion of this header file must take place in the program to use setw manipulator.

Also, below is a sample program to show the working of the setw manipulator.

                    
#include

#include

#include

using namespace std;

int main()

{

int num1,num2,num3;

cout<<"Enter three numbers:\n";

cin>>num1>>num2>>num3;

cout<<"\nDisplaying the three numbers\n"

<<"Num1:"<<setw(8)<<num1<
                

In the program above, the user is asked to input three integers and then the display takes place by using the setw() manipulator.

Pay attention to the manner of how the ‘<<‘ operator has been cascaded to display the three numbers. Moreover, use of the individual cout statements could have taken place for displaying each number.

FAQs For Use of endl and setw 

Question 1: Explain the purpose or use of setw in C++?

Answer 1: The C++ function std::setw behaves as if member width were called with n as an argument on the stream on which its insertion or extraction takes place as a manipulator. Furthermore, its insertion or extraction can take place on input streams or output streams. Moreover, its use takes place to set the field width to be used on output operations.

Question 2: What is the use of endl in C++?

Answer 2: The use of C++ manipulator endl function takes place to insert a new line character and flush the stream. Furthermore, the working of endl manipulator is similar to ‘\n’ character in C++. Moreover, the printing of the output of the following statement takes place by it in the next line.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.