COSC 1315

Programming Fundamentals

Formatted Output

Revised:  May 21, 2007
By Richard G. Baldwin

File:  Pf 00106
Practice Text


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Discussion

The purpose of this lesson is to illustrate and to explain two forms of simple output formatting.

Fixed number of decimal digits

One program, named Formatting01, will be used to illustrate the formatting of text and floating-point data into a format with a fixed number of digits to the right of the decimal point.

Tabular data

The second program, named Formatting02, will be used to illustrate the formatting of floating-point data into a table with the number of digits to the right of the decimal point being the same in each column, but being different between the columns.

Will discuss in fragments

I will discuss the programs in fragments.  A complete listing of the program named Formatting01 is presented in Listing 8 near the end of the lesson.  A complete listing of the program named Formatting02 is presented in Listing 9 near the end of the lesson.

Full understanding beyond scope of the course

Unfortunately, the object-oriented programming concepts behind these formatting capabilities is probably well beyond your capability to understand at this point in your programming education.  If so, you will simply need to memorize the format and syntax involved so that you can use these capabilities in programs that you write.

The program named Formatting01

This program illustrates simple formatted output along with the inclusion of the file named iomanip.  Inclusion of this file is required for the types of formatting that will be illustrated in this lesson.

This program shows how to format floating-point output so as to print two digits beyond the decimal point.  This can be generalized to format floating-point data to any specified number of digits beyond the decimal point.

Program output

This program produces the following output on the computer screen:

10 divided by 3 to two decimal digits is: 3.33

The numbers 10 and 3 are simply part of a literal string.  The decimal number at the end of the line of output text is the internal numeric value that is formatted to two decimal digits for printing.

Inclusion of the file named iomanip

The program code begins in Listing 1, with three compiler directives.  Only one of the compiler directives is new to this lesson, and it is shown in boldface.  This is the directive that specifies that the contents of the file named iomanip is to be inserted into the source code prior to attempting to compile the program.

#include <iostream>
#include <iomanip>
using namespace std;

Listing 1

Why do we need the file named iomanip?

Here is what one source has to say about this include file:

"This include file defines a set of manipulators that can affect both input and output streams of the iostream hierarchy of classes.  All these manipulators accept at least one parameter and mostly modify formatting information.  In order to be able to use these manipulators you have to explicitly include this file in your code."

What is a manipulator?

The so-called manipulators mentioned above are pretty much what the name implies.  C++ manipulators are capabilities that are peculiar to the C++ programming language that make it possible to manipulate the format of data in a variety of ways.

(Other languages such as C# and Java don't use manipulators.  Rather, they accomplish the same thing in different ways.)

The iomanip file is required

The contents of the include file named iomanip are required in order to use  the following manipulators, which are used in the two programs in this lesson:

There are many other manipulators that you can learn about here and here if you are interested in pursuing the topic further.

The main function

The main function for the program named Formatting01 begins in Listing 2.

int main(){
  cout << setprecision(2) << fixed << showpoint;

Listing 2

The statement in Listing 2 uses manipulators (highlighted in boldface) to prepare the system to:

Print the data

The boldface statement in Listing 3 prints:

  cout << "10 divided by 3 to two decimal digits is: " 
                                               << 10.0/3.0;

  return 0;
}//end main

Listing 3

As mentioned earlier, the program prints the following:

10 divided by 3 to two decimal digits is: 3.33

End of the main function

Listing 3 also contains a return statement that is included solely to satisfy the value-returning requirements of this version of the main function.  Listing 3 also signals the end of the main function and the end of the program.

You should make sure that you understand how the statements in the main function produce the output shown above before continuing.

The program named Formatting02

The program named Formatting02 begins in Listing 4, which shows the comments at the beginning of the program.

/* File: Formatting02.cpp
Copyright 2006, Richard G. Baldwin
Illustrates formatting output data into a table.  The
output is:

       1.5       2.200
      12.8       3.142
**********************************************************/

Listing 4

Print data in a table format

As indicated in those comments, this program differs from the program named Formating01 in that this program formats the output into a table consisting of two rows and two columns.  The new material that is presented in this program is the ability to format the data into the columns and rows that make up a table.

Compiler directives

Listing 5 shows the compiler directives, which are the same as in the previous program.

#include <iostream>
#include <iomanip>
using namespace std;

Listing 5

The main function

Listing 6 shows the beginning of the main function, including the code to print the first row of data in the desired tabular format.

int main(){
  //Print the first row in the table consisting of two
  // columns.
  cout << setw(10) << setprecision(1) << fixed << 1.5;
  cout << setw(12) << setprecision(3) << fixed << 2.2
                                                   << endl;

Listing 6

The new material

The new material in Listing 6 is the use of the setw manipulator to set the width of each of the output columns.  The use of the endl manipulator to end the current line and go the beginning of a new line is also new.

Different column widths and precision

Note that the first column is set to be 10 characters wide and the second column is set to be 12 characters wide.  Note also that the data to be printed in the first column is to be printed with 1 decimal digit, while the data to be printed in the second column is to be printed with 3 decimal digits.

Printing the values

The first statement in Listing 6 prints the value 1.5 in the specified format for the first column and leaves the output cursor on the same line at the end of the data that was printed.

The second statement in Listing 6 prints the value 2.2 in the specified format for the second column, and then uses the endl manipulator to cause the cursor to move down to the left side of the next line.

Print the second row of data

The code in Listing 7 prints the two values in the second row according to the same format specifications.

  //Print the second row.
  cout << setw(10) << setprecision(1) << fixed << 12.78;
  cout << setw(12) << setprecision(3) << fixed << 3.14159
                                                   << endl;
  return 0;
}//end main

Listing 7

The screen output

As mentioned earlier, this program produces the screen output shown in Figure 1.

       1.5       2.200
      12.8       3.142
Figure 1

You should make sure that you understand how this program uses manipulators to produce the tabular output shown in Figure 1.

Would normally use a loop structure

At this point, I need to point out that for a table of any size, it would be highly unusual to simply use sequential print statements to print the table.  Rather, it would be much more typical to use a loop structure.  However, since we haven't studied loop structures yet, I couldn't use them in this sample program.

It is important to note, however, that even if I had used a loop structure, the code in the body of the loop would have been very similar to the code in this program, at least insofar as the use of manipulators is concerned.

Complete Program Listings

Complete listings of the programs discussed in this lesson are provided below.

/* File: Formatting01.cpp
Copyright 2006, Richard G. Baldwin

Illustrates formatted output and the inclusion of <iomanip>

Shows how to format floating-point output to two decimal
digits.

The output is:

10 divided by 3 to two decimal digits is: 3.33
**********************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
  cout << setprecision(2) << fixed << showpoint;
  cout << "10 divided by 3 to two decimal digits is: " 
                                               << 10.0/3.0;

  return 0;
}//end main

Listing 8

 

/* File: Formatting02.cpp
Copyright 2006, Richard G. Baldwin
Illustrates formatting output data into a table.  The
output is:

       1.5       2.200
      12.8       3.142
**********************************************************/
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

  //Print the first row in the table consisting of two
  // columns.
  cout << setw(10) << setprecision(1) << fixed << 1.5;
  cout << setw(12) << setprecision(3) << fixed << 2.2
                                                   << endl;
  //Print the second row.
  cout << setw(10) << setprecision(1) << fixed << 12.78;
  cout << setw(12) << setprecision(3) << fixed << 3.14159
                                                   << endl;
  return 0;
}//end main

Listing 9


Copyright 2006, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-