C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, you can use the std::cout
object from the iostream
library to display formatted output to the console. By using various I/O manipulators and format flags, you can control the appearance of the output.
In this tutorial, we'll cover some ways to format the output with std::cout
.
std::cout
, include the iostream
and iomanip
headers.#include <iostream> #include <iomanip>
setw
and setfill
manipulators from the iomanip
library. The setw
function sets the field width, and setfill
sets the fill character.Example:
#include <iostream> #include <iomanip> int main() { int num1 = 42; int num2 = 123; int num3 = 7; std::cout << std::setw(5) << std::setfill('0') << num1 << std::endl; std::cout << std::setw(5) << std::setfill('0') << num2 << std::endl; std::cout << std::setw(5) << std::setfill('0') << num3 << std::endl; return 0; }
Output:
00042 00123 00007
setprecision
, fixed
, and scientific
manipulators.Example:
#include <iostream> #include <iomanip> int main() { double num1 = 42.123456789; double num2 = 123.987654321; std::cout << "Default precision: " << std::endl; std::cout << num1 << std::endl; std::cout << num2 << std::endl; std::cout << "Fixed format with 3 decimal places: " << std::endl; std::cout << std::fixed << std::setprecision(3) << num1 << std::endl; std::cout << std::fixed << std::setprecision(3) << num2 << std::endl; std::cout << "Scientific format with 5 decimal places: " << std::endl; std::cout << std::scientific << std::setprecision(5) << num1 << std::endl; std::cout << std::scientific << std::setprecision(5) << num2 << std::endl; return 0; }
Output:
Default precision: 42.1235 123.988 Fixed format with 3 decimal places: 42.123 123.988 Scientific format with 5 decimal places: 4.21235e+01 1.23988e+02
true
and false
strings by using the boolalpha
manipulator.Example:
#include <iostream> #include <iomanip> int main() { bool flag1 = true; bool flag2 = false; std::cout << "Without boolalpha: " << std::endl; std::cout << flag1 << std::endl; std::cout << flag2 << std::endl; std::cout << "With boolalpha: " << std::endl; std::cout << std::boolalpha << flag1 << std::endl; std::cout << std::boolalpha << flag2 << std::endl; return 0;
How to format output with cout in C++:
std::cout
for output in C++ and how to format simple text and values.#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
Using setw() with cout in C++:
std::setw
manipulator to set the field width for the output.#include <iostream> #include <iomanip> int main() { int number = 42; std::cout << std::setw(10) << number << std::endl; return 0; }
Formatting numbers with cout in C++:
std::cout
, including precision and fixed-point notation.#include <iostream> #include <iomanip> int main() { double value = 3.14159; std::cout << std::fixed << std::setprecision(2) << value << std::endl; return 0; }
Formatting strings with cout in C++:
std::cout
.#include <iostream> #include <iomanip> int main() { std::string text = "Hello"; std::cout << std::setw(10) << std::left << text << std::endl; return 0; }
Formatting dates and times with cout in C++:
std::cout
.#include <iostream> #include <iomanip> #include <chrono> #include <ctime> int main() { auto now = std::chrono::system_clock::now(); std::time_t currentTime = std::chrono::system_clock::to_time_t(now); std::cout << std::put_time(std::localtime(¤tTime), "%Y-%m-%d %H:%M:%S") << std::endl; return 0; }
Scientific notation with cout in C++:
#include <iostream> #include <iomanip> int main() { double value = 1234567.89; std::cout << std::scientific << value << std::endl; return 0; }
Formatting booleans with cout in C++:
std::boolalpha
in C++ cout.#include <iostream> #include <iomanip> int main() { bool flag = true; std::cout << std::boolalpha << flag << std::endl; return 0; }
Formatting enums with cout in C++:
std::cout
.#include <iostream> enum Color { RED, GREEN, BLUE }; int main() { Color color = GREEN; std::cout << color << std::endl; // Outputs the numeric value of the enum return 0; }
C++ cout hex and oct formatting:
#include <iostream> #include <iomanip> int main() { int value = 42; std::cout << std::hex << value << std::endl; // Outputs in hexadecimal return 0; }
Customizing cout output in C++:
#include <iostream> #include <iomanip> int main() { double value = 123.456; std::cout << std::setw(10) << std::fixed << std::setprecision(2) << value << std::endl; return 0; }