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

cout Formatted Output in C++

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.

  • Include the required headers: To work with std::cout, include the iostream and iomanip headers.
#include <iostream>
#include <iomanip>
  • Setting Width and Alignment: You can set the width and alignment of the output using the 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
  • Formatting Floating-Point Numbers: You can control the precision and format of floating-point numbers using the 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
  • Formatting Boolean Values: You can display boolean values as 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;
  1. How to format output with cout in C++:

    • Description: Introduces the basic usage of std::cout for output in C++ and how to format simple text and values.
    • Example Code:
      #include <iostream>
      
      int main() {
          std::cout << "Hello, World!" << std::endl;
          return 0;
      }
      
  2. Using setw() with cout in C++:

    • Description: Demonstrates the use of the std::setw manipulator to set the field width for the output.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          int number = 42;
          std::cout << std::setw(10) << number << std::endl;
          return 0;
      }
      
  3. Formatting numbers with cout in C++:

    • Description: Illustrates various ways to format numeric output using std::cout, including precision and fixed-point notation.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          double value = 3.14159;
          std::cout << std::fixed << std::setprecision(2) << value << std::endl;
          return 0;
      }
      
  4. Formatting strings with cout in C++:

    • Description: Shows how to format and align strings using std::cout.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          std::string text = "Hello";
          std::cout << std::setw(10) << std::left << text << std::endl;
          return 0;
      }
      
  5. Formatting dates and times with cout in C++:

    • Description: Demonstrates formatting date and time values for output using std::cout.
    • Example Code:
      #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(&currentTime), "%Y-%m-%d %H:%M:%S") << std::endl;
      
          return 0;
      }
      
  6. Scientific notation with cout in C++:

    • Description: Shows how to use scientific notation for outputting numbers in C++ cout.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          double value = 1234567.89;
          std::cout << std::scientific << value << std::endl;
          return 0;
      }
      
  7. Formatting booleans with cout in C++:

    • Description: Demonstrates formatting boolean values using std::boolalpha in C++ cout.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          bool flag = true;
          std::cout << std::boolalpha << flag << std::endl;
          return 0;
      }
      
  8. Formatting enums with cout in C++:

    • Description: Illustrates how to format enum values for output using std::cout.
    • Example Code:
      #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;
      }
      
  9. C++ cout hex and oct formatting:

    • Description: Introduces hexadecimal and octal formatting options for output in C++ cout.
    • Example Code:
      #include <iostream>
      #include <iomanip>
      
      int main() {
          int value = 42;
          std::cout << std::hex << value << std::endl;  // Outputs in hexadecimal
          return 0;
      }
      
  10. Customizing cout output in C++:

    • Description: Shows how to customize cout output using various manipulators and formatting options.
    • Example Code:
      #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;
      }