Source of reals_text.cpp


  1: //reals_text.cpp
  2: //Illustrates real number (floating point) and text output
  3: //combined, as well as declaration of, and assignment to, a
  4: //real number variable (a variable of data type "double").

  6: #include <iostream>
  7: #include <iomanip>
  8: using namespace std;

 10: int main()
 11: {
 12:     cout << "\nThis program displays some floating point values, with "
 13:         "and without text.\nStudy the output and the source code to see "
 14:         "how such values are displayed.\n\n";

 16:     cout << 2.345     << endl
 17:          << 2.3456    << endl
 18:          << 2.34567   << endl
 19:          << 2.345678  << endl
 20:          << 11111.1   << endl
 21:          << 111111.1  << endl
 22:          << 1111111.1 << endl << endl;

 24:     cout.setf(ios::fixed, ios::floatfield);
 25:     cout.setf(ios::showpoint);

 27:     cout << setw(8) << setprecision(1) << 2.3456 << endl;
 28:     cout << setw(1) << setprecision(2) << 2.3456 << endl;
 29:     cout << setw(1) << setprecision(8) << 2.3456 << endl;
 30:     cout << setw(1) << setprecision(8) << 1.23456789 << endl;
 31:     cout << setw(1) << setprecision(8) << 123456789.123456789 << endl;
 32:     cout << endl;

 34:     double price1;  //<-- This is a "declaration".
 35:     price1 = 2.95;  //<-- This is an "assignment".
 36:     double price2 = 1099.50;  //<-- This is an "initialization".
 37:     cout << "The first price is $"
 38:          << setw(1) << setprecision(2) << price1 << ".\n"
 39:          << "The second price is $"
 40:          << setw(1) << setprecision(2) << price2 << ".\n";
 41:     cout << endl;
 42: }