/* File: fig05_06.cpp
 * Info: Compound interest calculator.
 * Note: In real life programs, the use of float and double data types is discourage
 *       when performing monetary programs.  This imprecision can cause errors
 *       and possibly security issues. (i.e. "Salami slicing".)
 */
#include <iostream>	// cout, endl, fixed
#include <iomanip>	// setw, setprecision
#include <cmath>	// pow

int main(){
 double amount;		// Amount deposited at the end of the year.
 double principal;	// Initial amount before interest;
 double rate;		// Interest rate.
 int    years;		// number of years.
 double apr;		// rate + 1;

 std::cout << "Enter the principal (inital) amount: ";
 std::cin >> principal;
 std::cout << "Enter the annual interest rate: ";
 std::cin >> rate;
 std::cout << "Enter the number of years: ";
 std::cin >> years;

 apr = 1.0 + rate;

 std::cout << "Year" << std::setw(21) << "Amount on deposit" << std::endl;
 std::cout << std::fixed << std::setprecision(2);	// "magic formula"

 for(int year = 1; year <= years; year++)
 {
  amount = principal * std::pow(apr,year);
  std::cout << std::setw(4)  << year
            << std::setw(21) << amount
            << std::endl;
 };

 return 0;
};

