CS 1440 Lab 6

Cover sheetPrevious ActivityNext Activity


Activity 6-1: Prettier Output

Activity Goals

Understanding the subtle features of "formatted" output in C++

         I/O manipulators

         Precision of fractional numbers

         Columns of data

Activity Procedure

Pretty Output of Double Values

First, let's see what happens when we don't do anything extra.  Copy and paste the following program into a file called pretty.C and then compile and run it.

//  pretty.C
#include <iostream>
int main() {
     double  money = 100.0;
     cout  <<  "My share is "  <<  money / 3  <<  endl;
     cout  <<  "Her share is "  <<  money / 2  <<  endl;
}

So, you should notice that "my share" has a lot of fractional digits and "her share" is a whole number.  Now, if this were shares of money, then we know we want dollars and cents style output.  Specifically, two digits after the decimal point.  Well, we've got LOTS of problems as it is.  We're getting too many digits after the decimal point sometimes (my share) and not even getting a decimal point at all (her share).  Let's fix the second problem first.  Add the following statement BEFORE the cout of the shares.

cout.setf(ios::showpoint);

Now, compile and run the program.  See, that now we always get a decimal point, even for double values that have a zero fractional part.  Okay, so now we want to get exactly two digits after the decimal point.  Add the following statement BEFORE the cout of the shares (it can be before or after the "showpoint" statement you just added).

cout.precision(4);

Ah.  Now that is nicer.  Modify your program to change the value of money to 1000.0  instead of 100.0  and compile and run it.  Did you see that now we have an incorrect number of digits after the decimal point again?  If we change  cout.precision(4)  to cout.precision(5)  it will work again, but this is bad because how can we know when we write the program how big money might be?  Okay change  cout.precision(4)  to cout.precsion(2) and add the following statement BEFORE the cout of the shares.

cout.setf(ios::fixed);

So, what is going on?  First, you are telling the cout output stream variable to show decimal points.  Then you told the cout output stream to use a certain number of precision digits.  But this was for the entire number, not just after the decimal point.  Lastly you told the cout output stream you wanted to fix,or hold constant, the precision of the digits after the decimal point.

These commands to the cout output stream stay in effect until changed again by the program.  That is why they had to appear BEFORE the cout output statements themselves.  So you could use precision of 2 for my share and precision of 0 for her share by doing the following:

cout.precision(2);
cout <<  "My share " <<  money/3 << endl;
cout.precision(0);
cout <<  "Her share " <<  money/2 << endl;

The changes you made to cout above were accomplished by calling functions.  Recall that cout is special variable of a non-base type in C++.  There is another way to change the precision (but not the showpoint or fixed).  This alternative way uses a manipulator.  To use manipulators you must include the iomanip  header file.  Then you insert the manipulator directly into the output stream.  For example,

#include  <iomanip>
       . . .
       . . .
     cout << "My share "
          << setprecision(2) << money/3
          << endl;
     cout << "Her share "
          << setprecision(0) << money/2
          << endl;
       . . .

 

Output Lined Up in Columns

Copy and paste the following program into a file called columns.C and run and compile it.

// columns.C
#include <iostream>
int main() {
     int a=34, b=1, c=9876;
     cout << "\t  " << a << endl;
     cout << "\t+ " << b << endl;
     cout << "\t+ " << c << endl;
     cout << "\t------" << endl;
     cout << "\t  " << a+b+c << endl;
}

Is that how you remember writing your sums in grade school?  Me neither.  So how can we get it to line up correctly (on the rightmost digit)?  We can tell cout to output into a certain width "field" and pad the field that may not be used by our value.  We'll use the setw manipulator to set the width.  Change the first cout statement to the following.

     cout << "\t  " << setw(4) << a << endl;

Now compile and run it again.  What happened.  So, add manipulators before each of the values (b, c, a+b+c).  Unlike the precision( ) manipulator above that keeps the setting, the setw( ) manipulator does not.  So, you must use the manipulator IMMEDIATELY before each value you want manipulated (in a field in this case).  Compile and run this correct looking addition.

Activity Followup

Nothing here.  The postlab will have you use columns in your output!