codescracker


c++

C++ Formatting Output



« Previous Tutorial Next Tutorial »


Formatting output in C++, is important in the development of the output screen, which can be easily read and understood. C++ offers the programmer several input/output manipulators. Two of these (widely used) I/O manipulators are:

In order to use these manipulators, you must include the header file named iomanip.h. Here is an example, showing how to include this header file in your C++ program.

#include<iomanip.h>

The setw() Manipulator

In C++, the setw() manipulators sets the width of the field assigned for the output. It takes the size of the field (in number of characters) as parameter. Here is an example, this code fragment:

cout<<setw(6)<<"R";

generates the following output on the screen (each underscore represents a blank space).

_ _ _ _ _R

The setw() manipulator does not stick from one cout statement to the next. For example, if you want to right-justify three numbers within an 8-space field, you will need to repeat setw() for each value, as it shown below:

cout<<setw(8)<<22<<"\n";
cout<<setw(8)<<4444<<"\n";
cout<<setw(8)<<666666<<endl;

The output will be (each underscore represents a blank space):

_ _ _ _ _ _ 2 2
_ _ _ _ 4 4 4 4
_ _ 6 6 6 6 6 6

C++ Formatting Output Example

Here are some example program demonstrating, how to format the output screen in C++

/* C++ Formatting Output - The setw() Manipulator */

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, num;
	cout<<"Enter a number: ";
	cin>>num;
	cout<<"\nTable of "<<num<<" is:\n\n";
	for(i=1; i<=10; i++)
	{
		cout<<num<<setw(3)<<"*"<<setw(4)<<i<<setw(4)<<"="<<setw(4)<<num*i<<"\n";
	}
	getch();
}

Here is the sample run of the above C++ program:

c++ formatting output

Here another type of C++ program, also demonstrating, output formatting in C++

/* C++ Formatting Output - The setw() Manipulator */

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
	clrscr();
	int i;
	long int num;
	cout<<"Enter a number: ";
	cin>>num;
	cout<<"\nMultiplying (by 5) and printing the number 10 times with 3 columns:\n";
	for(i=0; i<10; i++)
	{
		cout<<num<<setw(25)<<num<<setw(25)<<num<<"\n";
		num = num * 5;
	}
	getch();
}

Here is the sample run of this C++ program:

output formatting c++

The setprecision() manipulator

In C++, the setprecision() manipulator sets the total number of digits to be displayed when floating-point numbers are printed. Here is an example, this code fragment:

cout<<setprecision(5)<<123.456;

will print the following output to the screen (notice the rounding) :

123.46

The setprecision() manipulator can also be used to set the number of decimal places to be displayed. In order for setprecision() to accomplish this task, you will have to set an ios flag. The flag is set with the following statement :

cout.setf(ios::fixed);

Once the flag has been set, the number you pass to setprecision() is the number of decimal places you want displayed. The following code:

cout.setf(ios::fixed);
cout<<setprecision(5)<<12.345678;

generates the following output on the screen (notice no rounding):

12.34567

Additional IOS flags

In the statement:

cout.setf(ios::fixed);

"fixed" i.e., ios::fixed is referred to as a format option. Other possible format options can be one of the following :

Format Value Meaning
left left-justify the output
right right-justify the output
showpoint displays decimal point and trailing zeros for all floating point numbers, even if the decimal places are not needed
uppercase display the "e" in E-notation as "E" rather than "e"
showpos display a leading plus sign before positive values
scientific display floating point numbers in scientific ("E") notation
fixed display floating point numbers in normal notation - no trailing zeroes and no scientific notation

You can remove these options by replacing setf(used with cout, recall cout.setf) with unsetf. For example, to get 5.8 to display as 5.80, the following lines of code are needed :

// display money
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout<<setprecision(2);
cout<<5.8;

Please note that all the subsequent couts retain the precision set with the last setprecision(). That means setprecision() is "sticky". Whatever precision you set, sticks with the cout device until such time as you change it with an additional setprecision() later in the program.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test