Learn C++ manipulators like endl and setw with beginner-friendly examples. Understand how to format output, align columns, and improve console display in C++ programs.
C++ manipulators are special tools used with cout to control or format the output.
In this lesson, we will learn two commonly used manipulators:
endl – moves the cursor to the next line.setw – sets the width of the output field.endlendl?endl is used with cout to:
cout << "Text" << endl;
Question:
Write a C++ program that prints two lines of text using the manipulator endl.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++ beginners!" << endl;
cout << "This line is printed using endl.";
return 0;
}
Hello, C++ beginners!
This line is printed using endl.
cout << "Hello, C++ beginners!" << endl;
cout displays the text."Hello, C++ beginners!" is the text to display.endl moves the cursor to the next line.Therefore, the second cout statement starts printing from a new line.
endl is mainly used to move output to the next line.
setwsetw?setw means set width.
It is used to set the minimum width of the output field. It is especially useful when displaying data in columns or tables.
setw is available in the <iomanip> library.
Therefore, we need to include:
#include <iomanip>
setw(number)
For example:
cout << setw(10) << 25;
This gives the output field a width of 10 characters.
setw to Create a TableQuestion:
Write a C++ program that prints a simple table of numbers using setw to align the output in columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(10) << "Number"
<< setw(10) << "Square" << endl;
cout << setw(10) << 2
<< setw(10) << 4 << endl;
cout << setw(10) << 5
<< setw(10) << 25 << endl;
cout << setw(10) << 10
<< setw(10) << 100 << endl;
return 0;
}
Number Square
2 4
5 25
10 100
Consider this statement:
cout << setw(10) << 2
<< setw(10) << 4 << endl;
setw(10) gives the first value a field width of 10 characters.2 is displayed within that field.setw(10) gives the next value another field width of 10 characters.4 is displayed within the second field.endl moves the cursor to the next line.This helps us display values in aligned columns.
setwsetw() applies only to the next output item.
For example:
cout << setw(10) << 25 << 50;
Here, setw(10) applies to 25 only. It does not automatically apply to 50.
If we want both values to have a width of 10, we write:
cout << setw(10) << 25
<< setw(10) << 50;
| Manipulator | Purpose | Header File |
|---|---|---|
endl |
Moves output to the next line | <iostream> |
setw() |
Sets the width of the next output field | <iomanip> |
endl → New linesetw() → Set output widthsetw() requires <iomanip>setw() affects only the next output itemendlWrite a C++ program that displays your:
Use endl to print each item on a separate line.
setwWrite a C++ program to display the following table using setw(10):
Number Cube
2 8
3 27
4 64
5 125
Write a C++ program that uses both endl and setw() to display a simple student information table containing: