Named Constants

 

A value that never changes is a constant.  Ex. P = ~3.14

 

Interest rate = 6%

 

We can represent these values with a named constant.  The value of a named constant cannot be changed during the execution of a program.

 

 

Syntax:   const <datatype> <constant name> = value;

 

Ex. const float INTEREST = .06;

 

By convention we capitalize the identifier for a named constant.

 

Ex. Create a program that prompts the user for a dollar amount and outputs the interest earned in a year when the interest rate is 6%.  Use a named constant for the interest.

 

Step1:  Basic analysis -  Inputs: interest rate, dollar amount

                                      Outputs: amount of interest in one year

          Extended: formula

                                      amountint = dollaramt * interest

 

Step2:   1. Use named constant for interest = 6%.

2. Prompt user for dollar amount.

3. Calculate interest

4. Print results

 

Step3: Code the solution


#include <iostream>

using namespace std;

 

int

main()

{

     const float INTEREST = 0.06;

 

     float amount;

     float amountint;

 

     cout << "Welcome to OUR NATIONAL BANK.  Our interest today is: "

<< INTEREST << endl;

     cout << "Please enter a dollar amount:  $";

     cin >> amount;

 

     amountint = amount * INTEREST;

 

     cout << endl << endl;

     cout << "By saving with us, in one year you will earn: $"

<< amountint <<endl;

 

     return 0;

}

 

 

Welcome to OUR NATIONAL BANK.  Our interest today is: 0.06

Please enter a dollar amount:  $23.75

 

 

By saving with us, in one year you will earn: $1.425

Press any key to continue

 

We do not usually display dollar amounts to 3 decimal places. 

 

Formatting float numbers:

 

Default:  Numbers are displayed in a field size that will fit the number exactly.  Floating point numbers are displayed to a default of 6 places before the decimal.  Any number with more than 6 places before the decimal is displayed in exponential notation


Ex.

 

float num1;

 

num1 = 123456;

cout << num1 << endl;  //displays  123456

 

num1 = 1234567;

cout << num1 << endl; // displays 1.23457e+006

 

We can use field width manipulators to format numbers printed by cout.

The field width manipulators are found in iomanip.h.  We must include this library in our programs.

 

Field width manipulators:

 

1. setw(n)  Places the number in a field width of n spaces.

 

Ex.

int num2;

 

num2 = 1234;

cout << '|'  << setw(7)

<< num2 << '|' << endl; //displays |   1234|

cout << '|' << setw(5)

 << num2 << '|' << endl; //displays | 1234|

 

 

If a field width is specified that is too small for the number, the number is printed in a field that is large enough to hold that number.

 

 

cout << '|' << setw(2)

 << num2 << '|' << endl; //displays |1234|

 

 

Numbers, by default,  are right justified in the field width. (flush to right margin)

 

setw is not persistent.  It only operates on the next piece of data on output stream.  Therefore if you want more than one output value placed in a specified field width, you must put setw in front of each.

 

 

2. setprecision(n)   - Sets floating point precision to n decimal places. For float numbers the precision indicates the number of digits displayed.

 

float num1;

 

num1 = 1234567;

cout << setprecision(3) << num1 << endl;  // 1.23e+006

 

The minimum number of digits displayed is 1.

 

setprecision is persistent. 

 

The default precision is 6.

 

3. Other ostream manipulators

 

showpoint - always displays the number with a decimal point with a default precision of 6.  If the integer portion needs more than 6 digits, the number is displayed in exponential format.

 

 

fixed - displays number always in fixed decimal format.  When fixed flag is on, precision refers to number of decimal places displayed.

 

scientific - displays in exponential format.

 

left - displays left justified in field

right - displays right justified in field.