C++

Output Formatting in C++

This article is about output formatting in C++.  Output formatting is a flexible implementation in the programming language to show the output in a properly structured and detectable manner. In C++, the display of the output syntax is very simple and easy for the user to understand. Output formatting also improves the user experience, and the user can easily read the bug message clearly on the output screen. The output manipulation is only possible from the “iomanip” or “iostream” header files in C++. Here, we will cover almost every aspect of output formatting with the help of detailed examples.

Basics of Output Stream in C++

The standard library is specially designed for input and output operations named the “iostream” header in C++. The fundamental standard output stream object from the header that is used for output is “std::cout”. We will use this to implement the formatting by applying the required specifiers.  This stream object sends relevant data to the standard output console.

Example 1: Display the Data on the Console Screen

In this example, we will display any data that is taken on the console screen as an output using the “std::cout” formatting object. The code snippet related to this scenario is attached in the following:

#include <iostream>

int main()
{
int digit = 42;
 double numeric = 6.64535;
std::cout << "Show the digit value: " << digit << std::endl;
std::cout << "show the numeric value: " " numeric " std::endl;
return 0;
}

 

The standard library that contains the input/output streaming object is “iostream” which is enclosed in the included header. Without using this header in the code, we cannot use the “std::cout” statement for output. So, after adding this library to the code, we build the main function in which we make a logic for output formatting.  In the main function, we take two variables which are initialized as “digit with int data type” and “numeric with double data type”.

The value of the digit variable is “42” and the value of the numeric variable is “6.64535”. Now, we want to display the values of these two variables on the window console screen. Here, we use an output formatting statement “std::cout” that first displays the message. After that, “<< digit” takes the initialized value and “<<std::endl” means moving the cursor to the next line. In the next line, the same process follows.  Anything that is written inside the “std::cout” displays the same on the console screen. The output of this code is attached in the following:

As seen in the given output, the same text is displayed on the console screen that is written in “Std::cout” in the code snippet. Here, the value in the integer is displayed for the digit. In the next line, the numeric value is also shown in double as “6.64535”.

Example 2: Output Formatting Using Precision Manipulators

In this example, we will pick the manipulator function for precision as “setprecision()” in C++.  Manipulators are defined methods that are designed to change the formatting of data that is used with the output stream. The precision sets() are also manipulators that work with the number of decimal places for floating point numbers. This method sets the precision of the floating-point numbers after decimal.

The precision method is defined in the “<iomanip>” header file. The argument that passes in the methods decides the total digit of a significant figure in the given number. We can use the output formatting stream object “std::cout” to display the value of the integer on the console screen. The code snippet of this scenario is attached in the following:

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
    double digit = 345.233434;
    std::cout <<"the precision of the digit is: "<<setprecision(6)<<digit;  
}

 

Here, we define the required output-related header files at the top. In the main function, we take a variable whose data type is floating. After that, we pass the set precision method to the output stream object “cout” having the argument value and pass the digit variable along that.

Output:

The output of this code is attached in the following:

In setprecision (3), 3 means that only 3 significant figures are used after the decimal point.

Example 3:  Display the Output Date and Time Format

This is another example related to the output formatting in C++. We will learn how to easily display the date and time on the console screen in just one step. In C++, the format date and time are defined in “<iomanip>” and “<ctime>” headers. Here, the code snippet related to this scenario is attached in the following:

#include <iostream>
#include <iomanip>
#include <ctime>

int main() {
    std::time_t currentTime = std::time(nullptr);
    std::tm* localTime = std::localtime(&currentTime);
    std::cout << "Current Date: " << std::put_time(localTime, "%Y-%m-%d") << std::endl;
    std::cout << "Current Time: " << std::put_time(localTime, "%H:%M:%S") << std::endl;
    return 0;
}

 

In the headers, we add the libraries for output. We use “#include <ctime>” for date and time. In the main function, we take the “currentTime” variable and pass the “std::time(nullptr)” function that returns the current time as a “std::time_t” object as the parameter. This function retrieves the current time from the system and stores it in the “currentTime” variable as the number of seconds elapsed.  The “localTime” variable stores the local time using the “std::localtime” function. After that, we display the current date by passing the “std::put_time()” and the given “Y-%m-%d” format at the end of the line. In the second output line, we print the current time by passing the “%H: %M:%S” format at the end of the line.  This is the date and time output formatting that we define in the put_time() method in “std::cout”. The output of this function is mentioned as follows:

As we can see, the current date is displayed in the same format that is given in the code as “Year-Month-Day”. In the next line, the second line output is displayed as the current time of the system which is the same as the given format in code. This way, we can display the output formats differently.

Conclusion

To conclude, output formatting is an essential tool that contains the output stream objects to display these different formats on the console window for user visibility and understanding. The “std::cout”, manipulators, and other functions can make this concept more helpful. Remember that the definition of libraries that are necessary for output must be done in the program. The user can also take these examples for their self-understanding and apply them to their environment for better learning. The developers can easily develop the output screen with the help of output formatting.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content