Lab 5 - Formating output with printf and cout

Part 1: Formating with printf

The printf() function is the C-style method of printing output to the screen. Pure C programs will use printf() for output instead of cout. To use the printf() function, you have to include stdio.h and als make a function call. The function call can take multiple arguments. The first argument is always a format string. The format string can be either just a plain string similar to what we've sent to cout (in which case it is the only argument to printf) or a string with formating tokens embedded in it. The formating tokens dictate how to display certain datatypes. It takes the form:
%[-][0][col][.prec]type
The - character is optional and will left justify the text (right justify is the default if - is omited).
The 0 character is optional and will pad the unused space with 0's. If 0 is omitted, spaces will be used for pad characters.
"col" is an integer for the column width. It is optional. If left off, the column will be wide enough to hold the variable.
".prec" is an integer preceeded by a decimal point for the number of digits of precision to use for doubles and floats. It is optional. If left off, the default precision will be used.
The type is a single character that represents the datatype of the variable. It is d for an integer, f for a double (or float) and c for a character. There are other types available. See man printf for a list.

Examples:

Take the following program and input it into a file on Helios. You can copy and paste on the lab machines by highlighting the text with your mouse, switching over to your Helios terminal, starting vi (or pico) and going into insert mode and then clicking the scroll wheel to paste. Compile and run the program once you have pasted it over.
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
  int num1, num2;
  double dnum1, dnum2;

  printf("Enter two integers: ");
  cin >> num1 >> num2;
  printf("Enter two doubles: ");
  cin >> dnum1 >> dnum2;

  printf("%d %d %f %f.\n", num1, num2, dnum1, dnum2);
  printf("%10d %10d %10f %10f.\n", num1, num2, dnum1, dnum2);
  printf("%010d %-10d %10.2f %10.4f.\n", num1, num2, dnum1, dnum2);

  return 0;
}

Part 2: Formating with cout

The cout object can also be used to format output. The method is not as straight-forward as with printf. To format cout output, you must include the iomanip library. This will give you access to several functions, as detailed below: Look at the code in lab5_example.cpp. This code creates the following output using cout manipulators:
integer output
right justify: |         5|
 left justify: |5         |

double output (2 precision)
right justify: |      5.50|
 left justify: |5.50      |

A small table without borders:
Name      Integer   Double
Alice              5      6.20
Bob                8     13.34
Carol             12     19.00
Copy the file over to your current directory using the following command (note: there is a dot at the end of the command, this represents the current working directory is the destination of the copy command):
cp /home/fac/melissa/public_html/cs221-f09/lab5_example.cpp .
Compile and run the code. You should see output similar to above.

Part 3: Lab assignment

Recreate the output of lab5_example.cpp using printf() in a file called lab5.cpp. Email your completed lab5.cpp to my Helios account.