/*************************************************
Weight gain example
*************************************************/
#include <iostream>
using namespace std;

double gainloss(double B);
double rate(double B);
double compound(double r);

int main()
{
  // Simulate 5 years
  double B = 0.0;
  for(int i = 0; i < 5; i++)
  {
    B = gainloss(B);
    double r = rate(B);
    B = B*compound(r);
  }
  cout << B << endl;

  return 0;
}

/*******************************************
 ** Interacts with user to perform transaction
 ** and returns new weight balance.
 *******************************************/
double gainloss(double B)
{
  // Get type of gain/loss transaction
  char act;
  cout << "Enter l:loss g:gain ";
  cin >> act;

  // Get amount of gain/loss
  double A;
  cout << "Enter amount: ";
  cin >> A;

  // Get new weight amount
  if (act == 'l')
    B = B - A;
  else
    B = B + A;

  return B;
}

/*******************************************
 ** Computes rate based on weight balance.
 *******************************************/
double rate(double B)
{
  // Get # of 20 lbs above 130.
  int T = (B-130)/20;

  // Calc rate
  double r = 3 + T*0.5;
  if (r > 8)
    r = 8;

  return r;
}

/*******************************************
 ** Gives the weight of one pound after a
 ** year of monthly compounding annual rate r.
 *******************************************/
double compound(double r)
{
  // Sim. year with monthly compounding
  double R = r/100, total = 1.0;
  for(int i = 0; i < 12; i++)
    total = total*(1 + R/12);

  return total;
}